Thư viện : stdio.h

clearerr
Code:
 #include <stdio.h>
  #include <stdlib.h>

  int main(int argc, char *argv[]) {
    FILE *in, *out;
    char ch;

    if((in=fopen("inFile.txt", "rb")) == NULL) {
      printf("Cannot open input file.\n");
      exit(1);
    }
    if((out=fopen("outFile.txt", "wb")) == NULL) {
      printf("Cannot open output file.\n");
      exit(1);
    }

    while(!feof(in)) {
      ch = getc(in);
      if(ferror(in)) {
        printf("Read Error");
        clearerr(in);
        break;
      } else {
        if(!feof(in))
          putc(ch, out);
         
        if(ferror(out)) {
          printf("Write Error");
          clearerr(out);
          break;
        }
      }
    }
    fclose(in);
    fclose(out);

    return 0;
  }

fclose : Closes the file and flushes its buffer

Code:
#include <stdio.h>
  #include <stdlib.h>

  int main(void)
  {
    FILE *fp;

    if((fp=fopen("test", "rb"))==NULL) {
      printf("Cannot open file.\n");
      exit(1);
    }

    if(fclose(fp))
        printf("File close error.\n");

    return 0;
  }
feof: determines whether the end of the file has been reached
Code:
  #include <stdio.h>
  #include <stdlib.h>

  int main(void){
     FILE *fp;

     if((fp=fopen("test", "rb"))==NULL) {
        printf("Cannot open file.\n");
        exit(1);
     }

     while(!feof(fp)) {
        char ch = getc(fp);
        printf("%c",ch);
     }

     fclose(fp);
  }
ferror: check for a file error
Code:
 #include <stdio.h>
  #include <stdlib.h>

  int main(void){
     FILE *fp;

     if((fp=fopen("test", "rb"))==NULL) {
        printf("Cannot open file.\n");
        exit(1);
     }

     putc('C', fp);
     if(ferror(fp)) {
       printf("File Error\n");
       exit(1);
     }
    
     fclose(fp);
     return 0;
  }
fflush: force the buffer contents to be written to the file
Code:
  #include <stdio.h>
  #include <stdlib.h>

  int main(void){
     FILE *fp;

     if((fp=fopen("test", "rb"))==NULL) {
        printf("Cannot open file.\n");
        exit(1);
     }

      char ch = 'C';
      int i;
      for(i=0; i<5; i++) {
        fwrite(ch, sizeof(ch), 1, fp);
        fflush(fp);
      }
      fclose(fp);
      return 0;
  }
fgetc: gets a character from the stream and increments the file position pointer

Code:
  #include <stdio.h>
  #include <stdlib.h>

  int main(int argc, char *argv[])
  {
    FILE *fp;
    char ch;

    if((fp=fopen("test","r"))==NULL) {
      printf("Cannot open file.\n");
      exit(1);
    }

    while((ch=fgetc(fp)) != EOF) {
      printf("%c", ch);
    }
    fclose(fp);

    return 0;
  }
fgetpos: stores the current file position indicator

Code:
  #include <stdio.h>
  #include <stdlib.h>

  int main(int argc, char *argv[])
  {
      FILE *fp;
      fpos_t file_loc;

      if((fp=fopen("test","r"))==NULL) {
          printf("Cannot open file.\n");
          exit(1);
      }
   
      fgetpos(fp, &file_loc);

      fclose(fp);
  }
fgets: reads up to num-1 characters from stream and stores them in
*str

Code:
  #include <stdio.h>
  #include <stdlib.h>

  int main(int argc, char *argv[])
  {
    FILE *fp;
    char str[128];

    if((fp=fopen("test", "r"))==NULL) {
      printf("Cannot open file.\n");
      exit(1);
    }

    while(!feof(fp)) {
      if(fgets(str, 126, fp)) printf("%s", str);
    }

    fclose(fp);

    return 0;
  }
fopen: opens a file by *fname
Code:
//Mode             Meaning
//"r":             Open text file for reading 
//"w":             Create a text file for writing 
//"a":             Append to text file 
//"rb":            Open binary file for reading 
//"wb":            Create binary file for writing 
//"ab":            Append to a binary file 
//"r+":            Open text file for read/write 
//"w+":            Create text file for read/write 
//"a+":            Open text file for read/write 
//"rb+" or "r+b":  Open binary file for read/write 
//"wb+" or "w+b":  Create binary file for read/write 
//"ab+" or "a+b":  Open binary file for read/write 



  

  #include <stdio.h>
  #include <stdlib.h>

  int main(int argc, char *argv[])
  {
      FILE *fp;
   
      if ((fp = fopen("test", "w"))==NULL) {
        printf("Cannot open file.\n");
        exit(1);
      }
      fclose(fp);
  }
fprintf: outputs the values to the stream
Code:
#include <stdio.h>
  #include <stdlib.h>

  int main(void)
  {
    FILE *fp;

    if((fp=fopen("test", "wb"))==NULL) {
      printf("Cannot open file.\n");
      exit(1);
    }
    //The format control string are identical to those in printf();
    fprintf(fp, "this is a test %d %f", 10, 20.01);
    fclose(fp);

    return 0;
  }
fputc: writes a character to the stream
Code:
#include <stdio.h>
  #include <stdlib.h>

  int main(void)
  {
    FILE *fp;

    if((fp=fopen("test", "wb"))==NULL) {
      printf("Cannot open file.\n");
      exit(1);
    }

    char *str = "www.java2s.com";
    while(*str){
         if(!ferror(fp)) {
              fputc(*str++, fp);
         }
    }
 
    fclose(fp);
  }
fputs: writes a string to the stream(The string's null terminator is not written)
Code:
  #include <stdio.h>
  #include <stdlib.h>

  int main(void)
  {
    FILE *fp;

    if((fp=fopen("test", "wb"))==NULL) {
      printf("Cannot open file.\n");
      exit(1);
    }
 
    fputs("this is a test", fp);
    fclose(fp);
  }
fread: reads a number of objects by size and stores them in *buf
Code:
  #include <stdio.h>
  #include <stdlib.h>

  int main(void)
  {
    FILE *fp;
    float bal[5] = { 1.1F, 2.2F, 3.3F, 4.4F, 5.5F };
    int i;

    if((fp=fopen("test", "wb"))==NULL) {
      printf("Cannot open file.\n");
      exit(1);
    }

    if(fwrite(bal, sizeof(float), 5, fp) != 5)
      printf("File read error.");
      fclose(fp);

    if((fp=fopen("test", "rb"))==NULL) {
      printf("Cannot open file.\n");
      exit(1);
    }

    if(fread(bal, sizeof(float), 5, fp) != 5) {
      if(feof(fp)) {
         printf("Premature end of file.");
      }else {
         printf("File read error.");
      }
    }
    fclose(fp);

    for(i=0; i<5; i++){
      printf("%f ", bal[i]);
    }
    return 0;
  }

         
/*
1.100000 2.200000 3.300000 4.400000 5.500000 */
freopen: associates an existing stream with a different file
Code:
/Mode             Meaning
//"r":             Open text file for reading 
//"w":             Create a text file for writing 
//"a":             Append to text file 
//"rb":            Open binary file for reading 
//"wb":            Create binary file for writing 
//"ab":            Append to a binary file 
//"r+":            Open text file for read/write 
//"w+":            Create text file for read/write 
//"a+":            Open text file for read/write 
//"rb+" or "r+b":  Open binary file for read/write 
//"wb+" or "w+b":  Create binary file for read/write 
//"ab+" or "a+b":  Open binary file for read/write 


  


  #include <stdio.h>
  #include <stdlib.h>

  int main(void)
  {
    FILE *fp;

    printf("This will display on the screen.\n");

    if((fp=freopen("OUT", "w" ,stdout))==NULL) {
      printf("Cannot open file.\n");
      exit(1);
    }

    printf("This will be written to the file OUT.");

    fclose(fp);

    return 0;
  }
fscanf: works exactly like the scanf() function except that it reads from the stream
Code:
  
  #include <stdio.h>
  #include <stdlib.h>

  int main(void)
  {
    FILE *fp;
    float bal[5] = { 1.1F, 2.2F, 3.3F, 4.4F, 5.5F };
    int i;

    if((fp=fopen("test", "wb"))==NULL) {
      printf("Cannot open file.\n");
      exit(1);
    }

    if(fwrite(bal, sizeof(float), 5, fp) != 5){
      printf("File read error.");
    }
    fclose(fp);

    char str[80];
    float f;

    fscanf(fp, "%s%f", str, &f);
    fclose(fp);

  }
fseek: moves the file position pointer
Code:
//Name       Meaning
//SEEK_SET:  Seek from start of file 
//SEEK_CUR:  Seek from current location 
//SEEK_END:  Seek from end of file 


  #include <stdio.h>
  #include <stdlib.h>

  struct fullname {
    char firstName[40];
    char lastName[10];
  } info;

  int main(void){
    FILE *fp;

    if((fp=fopen("test", "rb")) == NULL) {
      printf("Cannot open file.\n");
      exit(1);
    }

    int client_num = 10;

    /* find the proper structure */
    fseek(fp, client_num*sizeof(struct fullname), SEEK_SET);

    /* read the data into memory */
    fread(&info, sizeof(struct fullname), 1, fp);

    fclose(fp);
  }
fsetpos: moves the file position pointer which is from fgetpos()
Code:
  #include <stdio.h>
  #include <stdlib.h>

  int main(int argc, char *argv[])
  {
      FILE *fp;
      fpos_t file_loc;

      if((fp=fopen("test","r"))==NULL) {
          printf("Cannot open file.\n");
          exit(1);
      }
   
      fgetpos(fp, &file_loc);

      fsetpos(fp, &file_loc);
     
      fclose(fp);
  }
ftell: returns the value for the file position pointer
Code:
 #include <stdio.h>
   #include <stdlib.h>

   int main(void){
      FILE *fp;

      if((fp=fopen("test", "rb")) == NULL) {
        printf("Cannot open file.\n");
        exit(1);
      }


      int i;
      if((i=ftell(fp)) == -1L)
         printf("A file error has occurred.\n");
      fclose(fp);
   }
fwrite: writes count number of objects
Code:
 #include <stdio.h>
  #include <stdlib.h>

  int main(void)
  {
    FILE *fp;
    float f=12.23;

    if((fp=fopen("test", "wb"))==NULL) {
      printf("Cannot open file.\n");
      exit(1);
    }

    fwrite(&f, sizeof(float), 1, fp);

    fclose(fp);

    return 0;
  }
getc: reads a character from stream

Code:
 
  #include <stdio.h>
  #include <stdlib.h>

  int main(int argc, char *argv[])
  {
    FILE *fp;
    char ch;

    if((fp=fopen("test", "r"))==NULL) {
      printf("Cannot open file.\n");
      exit(1);
    }

    while((ch=getc(fp))!=EOF) {
      printf("%c", ch);
    }

    fclose(fp);
    return 0;
  }
getchar: read a character from stdin
Code:
  #include <stdio.h>

  int main(void)
  {
    char s[256], *p;

    p = s;
    printf("input char:");
    while((*p++ = getchar())!= '\n');
    *p = '\0'; /* add null terminator */

    printf(s);

    return 0;
  }

         
/*
input char:123
123
*/
gets: reads characters from stdin
Code:
 #include <stdio.h>
  #include <stdlib.h>

  int main(void)
  {
    char fname[128];

    printf("Enter filename: ");
    gets(fname);

    printf("%s \n", fname);

    return 0;
  }

         
/*
Enter filename: 321
321
*/
perror: maps error message to the global variable errno and outputs that string to stderr
Code:
 #include <stdio.h>
  #include <stdlib.h>

  int main(void)
  {

    perror("File error ");
  }

         
/*
File error : No Error
*/
printf: display message by format
Code:
#include <stdio.h>

int main(void){
 printf("Hi %c %d %s", 'c', 10, "there!");
}
putc: writes a character to the output stream
Code:
  #include <stdio.h>
 
  int main(void){
 
    FILE *fp;

    if((fp=fopen("test", "w"))==NULL) {
      printf("Cannot open file.\n");
      exit(1);
    } 
 
    char *str = "www.java2s.com";
 
    for(; *str; str++){
        putc(*str, fp);
    }
  }

putchar: output character to stdout

Code:
#include <stdio.h>
int main(void){

   char *str = "www.java2s.com";

   for(; *str; str++){
       putchar(*str);
   }
}
puts: writes the string to the standard output device
Code:

#include <stdio.h>
#include <string.h>

int main(void)
{
char str[80];

strcpy(str, "this is an example");

puts(str);

return 0;
}
[/code]

remove: delete file by *fname
Code:
  #include <stdio.h>

  int main(int argc, char *argv[])
  {
    if(remove("test"))
           printf("Remove Error");

    return 0;
  }
rename: changes the file name
Code:
  #include <stdio.h>

  int main(int argc, char *argv[])
  {
    if(rename("oldName", "newName") != 0){
       printf("Rename Error");
    }
    return 0;
  }
rewind: moves the file position pointer back to the start
Code:
#include <stdio.h>
  #include <stdlib.h>

  int main(int argc, char *argv[])
  {
    FILE *fp;

    if((fp=fopen("test", "r"))==NULL) {
      printf("Cannot open file.\n");
      exit(1);
    }

    while(!feof(fp)){
        putchar(getc(fp));
    }
    rewind(fp);


    while(!feof(fp)){
        putchar(getc(fp));
    }
    fclose(fp);
  }
scanf: read input
Code:
 #include <stdio.h>

  int main(void)
  {
    char str[80], str2[80];
    int i;

    scanf("%79s", str); //scanf up to 79 chars into str

    return 0;
  }
setbuf: specifies the buffer that stream will use
Code:
#include <stdio.h>

int main ()
{
  char buffer[BUFSIZ];
  FILE *fp1, *fp2;

  fp1=fopen ("test.txt","w");
  fp2=fopen ("test2.txt","a");

  setbuf ( fp1 , buffer );
  fputs ("This is sent to a buffered stream",fp1);
  fflush (fp1);

  setbuf ( fp2 , NULL );
  fputs ("This is sent to an unbuffered stream",fp2);

  //Set buf to null to turn off buffering.
  //The buffer must be BUFSIZ characters long.
  //BUFSIZ is defined in <stdio.h>.  


  fclose (fp1);
  fclose (fp2);

  return 0;

}
setvbuf: sets the buffer for stream to be buffer, with a size of 'size_t size'
Code:
#include <stdio.h>

int main ()
{
  FILE *fp;

  fp=fopen ("myfile.txt","w");

  setvbuf ( fp , NULL , _IOFBF , 1024 );

  // File operations here

  fclose (fp);

  return 0;
}
snprintf: identical to sprintf() except that a maximum of num-1 characters will be stored
Code:
#include <stdio.h>

int main ()
{
  char buffer [50];
  int n, a=5, b=3;
  n=snprintf (buffer, 5,"%d plus %d is %d", a, b, a+b);
  printf ("[%s] is a %d char long string\n",buffer,n);
  return 0;
}
sprintf: is identical to printf() except that the output is a char array
Code:
#include <stdio.h>

int main(void){
  char str[80];

  sprintf(str,"%s %d %c", "one", 2, '3');

  printf("%s", str);
}
sscanf: identical to scanf() except that data is from *buf
Code:
  #include <stdio.h>

  int main(void)
  {
    char str[80];
    int i;

    sscanf("hello 1 2 3 4 5", "%s%d", str, &i);
    printf("%s %d", str, i);

    return 0;
  }
tmpfile: opens a temporary binary file for read/write operations and returns a pointer to the stream
Code:
#include <stdio.h>

int main(void){
  FILE *temp;
  if((temp=tmpfile())==NULL) {
    printf("Cannot open temporary work file.\n");
    exit(1);
  }
}

tmpnam: generates a unique filename

Code:
  #include <stdio.h>

  int main(void)
  {
    char name[40];
    int i;

    for(i=0; i<3; i++) {
      tmpnam(name);
      printf("%s ", name);
    }
    return 0;
  }
ungetc: Put a character back to the input stream
Code:
#include <stdio.h>

int main ()
{
  FILE * fp;
  int c;
  char buffer [256];
  fp = fopen ("test.txt","rt");
 
  if (fp==NULL)
    perror ("Error opening file");
  else {
    while (!feof (fp))
    {
      c=getc (fp);
      if (c == '#')
        ungetc ('@',fp);
      else
        ungetc (c,fp);
      fgets (buffer,255,fp);
      fputs (buffer,stdout);
    }
  }

  return 0; 
}
Hết thư viện .... Hết tiền luôn.. Cuối tháng đi làm lĩnh lương vào net tiếp T_T