File Handling Functions in C

Question: Which I/O Functions or Family of Functions deal with Individual Characters, Text Lines and Binary Data in C Language

Answer: ANSI C standard provides specific family or set of functions to perform I/O on different types of data. For Character I/O, getchar and putchar family of functions, for text data I/O we have gets and puts family of functions while for binary I/O, library provides fread and fwrite functions.

Members in each family of functions differ in where they obtain input from and where they write output to. For ex. getchar() and putchar() functions deal with individual characters and read from and write to stdin and stdout respectively. While pairs fgetc() and fputc(), getc() and putc() functions read from and write to files. These are prototyped below,

    int fgetc(FILE *stream);
    int getc(FILE *stream);
    int getchar(void);
 
    int fputc(int character, FILE *stream);
    int putc(int character, FILE *stream);
    int putchar(int character);

Similarly, functions that perform I/O on text data, read from stdin or some other file and/or write to stdout or some other file. I/O on text data may be formatted line I/O or unformatted line I/O. gets() and puts() family of functions deal with unformatted line I/O i.e. these functions deal with reading and writing strings. printf() and scanf() family of functions perform formatted line I/O. Functions
for unformatted line I/O are prototyped below

    char *fgets(char *buffer, int buffer_size, FILE *stream);
    char *gets(char *buffer);
 
    int fputs(char const *buffer, FILE *stream);
    int puts(char const *buffer);

Prototype of functions for formatted line I/O, firstly input functions

advertisement
advertisement
    int fscanf(FILE *stream, char const *format, ...);
    int scanf(char const *format, ...);
    int sscanf(char *buffer, char const *format, ...);

Notice the ellipsis in each function indicating a variable-length list of pointers. Values converted from input are stored one-by-one into locations pointed to by these arguments.

Functions for line output are prototyped below

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

Input/Output operations on binary data can be accomplished by a set of two functions prototypes of which are given below

advertisement
    size_t fread(void *buffer, size_t size, size_t count, FILE *stream);
    size_t fwrite(void *buffer, size_t size, size_t count, FILE *stream);

Notice that buffer is a pointer to area that holds data. size is size of element in the buffer array, count is no. of elements and stream, of course, is where to read from and write to.

We’ll discuss I/O of each type of data in detail in other posts related to I/O fuctions.

advertisement

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.