0
3.0kviews
Explain how to read the contents of file and write into the file with Syntax.
1 Answer
0
117views

Functions used to read the contents of file and write into the file.

Reading a File:

  • To read a file we use fgetc function.
  • Syntax: int fgetc( FILE * fp );
  • The fgetc() function reads a character from the input file referenced by fp.
  • The return value is the character read, or in case of any error it returns EOF(End of File).
  • The following functions allow you to read a string from a stream
  • Syntax: char *fgets( char * buf, int n, FILE * fp );
  • The functions fgets() reads up to n - 1 characters from the input stream referenced by fp. It copies the read string into the buffer buf, appending a null character to terminate the string.

Write into the file:

  • To write into a file we use fputc function.
  • Syntax: int fputc( int c, FILE *fp );
  • The function fputc() writes the character value of the argument c to the output stream referenced by fp.
  • It returns the written character written on success otherwise EOF if there is an error.
  • You can use the following functions to write a null-terminated string to a stream:
  • Syntax: int fputs( const char *s, FILE *fp );
  • The function fputs() writes the string s to the output stream referenced by fp.
  • It returns a non-negative value on success, otherwise EOF is returned in case of any error.
Please log in to add an answer.