Progrsmming C : stdio.h example 1
clearerr: Reset (set to zero) the error flag(The end-of-file indicator is also reset.)
//Declaration: void clearerr(FILE *stream); #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; }
feof: determines whether the end of the file has been reached
//Declaration: int feof(FILE *stream); //Return: A nonzero value: if the file pointer is at the end of the file. zero: otherwise. #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); }
fflush: force the buffer contents to be written to the file
//Header: #include <stdio.h> //Declaration: int fflush(FILE *stream); //Return: 0 on success or EOF on error. #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[] = "This is a text"; int i; for(i=0; i<5; i++) { fwrite(ch, sizeof(ch), 1, fp); fflush(fp); } fclose(fp); return 0; }
fgetpos: stores the current file position indicator
//Header: #include <stdio.h> //Declaration: int fgetpos(FILE *stream, fpos_t *position); //Return: returns zero on success or nonzero on failure #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); }
Open a file called TEST for binary read/write operations
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { FILE *fp; if((fp=fopen("test", "rb+"))==NULL) { printf("Cannot open file.\n"); exit(1); } fclose(fp); }
fopen: opens a file by *fname
//Header: #include <stdio.h>
//Declaration: FILE *fopen(const char *fname, const char *mode);
//Return: returns a FILE pointer on success or NULL pointer on failure
//The legal values for mode.
//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);
}fputc: writes a character to the stream
//Header file: #include <stdio.h> //Declaration: int fputc(int ch, FILE *stream); //Return: returns the value of the character written on success or EOF on failure. #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); }
fread: reads a number of objects by size and stores them in *buf
//Header file: #include <stdio.h> //Declaration: size_t fread(void *buf, size_t size, size_t count, FILE *stream); //Return: returns the number of items actually read. #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 */
fscanf: works exactly like the scanf() function except that it reads from the stream
//Header file: #include <stdio.h> //Declaration: int fscanf(FILE *stream, const char *format, ...); //Return: returns the number of arguments actually assigned values or EOF on failure. #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); }
fwrite: writes count number of objects
//Header file: #include <stdio.h> //Declaration: size_t fwrite(const void *buf, size_t size, size_t count, FILE *stream); //Return: returns the number of items written on success. Fewer items written means failure. #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; }