C Language -> Input and output functions summarize data on file calls

Data input output relationship between objects:

                                      

 

Function for use:

1. A character input \ output, the object is a keyboard (and screen buffer)

1.1.getchar(a),putchar(a);

1.2.scanf(“%d”,&i),printf(“%d”,i)

2. A variable input \ output, the object is a keyboard (and screen buffer)

2.1. gets(a),puts(a)

2.2.scanf(“%d %c %2f”,&i,&a,&b),printf(“%d %c %2f”,i,a,b)

 

3. Enter a character \ output object file (file and disk cache)

3.1.p=fgetc(fp),fput(p);p=getc(fp),putc(p)

 

4. a \ character channeling more input \ output object file (file and disk cache)

4.1.fgets(str,n,fp),fputs(str,fp)     

     Notes: // fgetc () function (getc () macro) and fputc () function (putc () macro)

getc () putc () macro, fgetc (), fputc () is a function, the relationship between them

#define putc(ch,fp) fputc(ch,fp)

#define getc(fp)    fgetc(fp)

Thus, getc (fp) is fgetc (ch, fp) defined macros, putc (ch, fp) is fputc (ch, fp) defined macros, as they realize the function

4.2. fread(buffer,size,count,fp),fwrite(buffer,size,count,fp)

     NOTE: fread (read the address data stored in the (buffer), the number of bytes to write (size), the number of read bytes of the data item size (count), the file type pointer (FP));

fwrite (address output data storage (buffer), the number of bytes to write (size), the number of read bytes of the data item size (count), the file type pointer (FP));

example:

fread (& stud [i], sizeof (struct student_type), 1, stdin); // read the data into the cache structure \ array variable, with printf () display data to the screen

fwrite(&stud[i],sizeof(struct student_type),1,stdin);

4.3. fscanf(fp,”%d,%c,%2f”,&i,&a,&b),fpringf(fp,”%d,%c,%2f”,i,a,b)

Note: The format limits the input \ output terminal to read and write but the object is not a disk file (recommended less), can be replaced with fread and fwrite, call format:

fscanf (file pointer, format string, output table column); // read the variable data from the cache to the disk file, by printf () display data to the screen ???

fprintf (file pointer, format string, output table columns);

例子:fscanf(fp,"%d,%f",&i,&t);

      fprintf(fp,"%d,%6.2f",i,t);

 

File structure:
typedef struct
{int -fd; // File No.
  int -cleft; // buffer remaining characters
  int -mode; // file operation mode
  char * -nextc; // the next character
  char * -buff; // file buffer location
  } fILE;

 

example:

#include"stdio.h" void main(){  

FILE *in,;  char *ch;  

// r + read / write open a text file, the file is non-empty file, put the end of the file position pointer position, need to add fseek (in, 0,2); Statement  

in = fopen ( "E: \\ \\ new folder new folder \\ a.txt", "r +"); // a file at the end of the additional data, and non-empty empty files are files in the file location pointer end    

// rewind (in); // the pointer back to the beginning of the file location

 fseek (in, 0,2); // the pointer back to the end of the file location

  // ch = c; // input string will produce garbled, assignment errors  

ch = & c [0];. // 1 pointer  

// scanf ( "% s", c);. // 2 using arrays  

gets(ch);  

fputs(ch,in); puts(ch);  

fclose(in);

}

 

Guess you like

Origin www.cnblogs.com/WE-ON-LINE/p/11666203.html