Input/Output operations on File In C

Once a file opened,reading from of writing to the file is accomplished using standard I/O routines that are listed in table -

img
Reading/Writing a Character

Both putc() and fputc() functions are used to write a character to file that was previously opened for writing. The prototype of putc () is

Int putc( int ch, FILE *fp);

Where fp is the file pointer and ch is the character to be output. If putc() operation is successful, it returns the character return,otherwise, returns EOF.

Similarly, both getc () and fgetc() functions are used to read a character from file that was previously opened for reading. The prototype of getc() is

Int getc(FILE *fp);

Where fp is the file pointer. Getc() returns an integer,but its high order byte is 0. This function returns EOF when an end-of-file is encountered.


Use of feof() Function

The feof() function can be used to test for an end-of-file condition. The prototype of the feof () function is given below:

Int feof(FILE *fp);

This function takes a FILE pointer as argument and returns an integer value.


Reading/Writing a string

In addition to getc () and putc(). C supports the related functions fputs() and fgets(), which write and read character strings to and from a file. These work just like putc () and getc(),but instead of writing and reading a single character, they write and read strings. The prototypes of these two functions are given below:

Input fputs (const char *str, FILE *fp);

Char *fgets (char *str, int length, FILE *fp);

The fgets() function reads a string from the specified stream until either a newline character is read or length-1 characters have been read. If a newline is read, it will be part of the string. The resultant string will be null terminated. The function returns a pointer to str if successful and a null pointer if an error occurs. But the fputs() function writes a string to a file.


Use of fscanf() and fprintf() Functions

The fscanf() and fprintf() functions beahave exactly like scanf () and printf (),except that operate with files. The prototypes of these two functions are given below;

Int fscanf(FILE *fp, const char *control_string,…);
Int fprintf(FILE *fp, const char *control_string,…);

Where fp is the file pointer.


Use of fread() and fwrite() functions

C file system provides two functions: fread() and fwrite() to perform read and write operations on file. The prototypes of these two functions are given below:

Size_t fread(void *buffer is a pointer to the region of memory that will receive the data from the file. For fwrite(), buffer is the pointer to the information that will be written to the file. The number of bytes to be read or written is specified by num_bytes. The argument count determines how many items are read or written. The type size_t is defined in stdio.h and is same as unsigned int. The fp is the FILE pointer. The fread() and fwrite() return the number of items they read and written respectively.



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext