Friday, September 10, 2021

File Management functions

A File can be used to store a large volume of persistent data. Like many other languages ‘C’ provides following file management functions:

  1. Creation of a file
  2. Opening a file
  3. Reading a file
  4. Writing to a file
  5. Close

Functions



Syntax



Description



fscanf( )



int fscanf (FILE *stream, const char *format,....);



It is used for reading the formatted data from the stream.



fgets( )



char *fgets(char *str, int size, FILE *stream);



It stands for file get string. It is used for getting the string from a stream.



fgetc( )



int fgetc (FILE *stream);



It will return the next character from the stream from the end of the file or an error.



fread( )



int fread(void *str, size_t size, size_t num, FILE *stream);



It is used for reading data from a file.



Following are the list of functions which are used for writing a file:


Functions



Syntax



Description



fprintf()



int fprintf (FILE *stream, const char * format,...);



It is used for writing the formatted output of the stream.



fputs()



int fputs(const char *str, FILE *stream);



It is used for writing a line to a file.



fputc()



int fputc(int c, FILE *stream);



It is opposite to fgetc() and is used for writing a character to the stream.



fwrite()



int fwrite(const void *str, size_t size, size_t count, file *stream);



It is used for writing data to a file.



Following are the functions which are used for detecting the errors:


Functions



Syntax



Description



clearerr()



void clearerr(FILE *stream);



It is used for clearing the end-of-file and error indicators for the stream.



perror()



void perror(char *msg);



It stands for the print error.



Closing a file :

After every successful file operation, always close a file.

For closing a file, fclose function is used.

Ex:

filePointer= fopen(“fileName.txt”, “w”);

---------- Some file Operations -------

fclose(filePointer)


0 comments:

Post a Comment

Data Structures with C++



NET/SET/CS PG



Operating Systems



Computer Networks



JAVA



Design and Analysis of Algorithms



Programming in C++

Top