You are here:   HomeC Programming (Theory)File Handling
by Dinesh Thakur Category: File Handling

The getw and putw functions, The fprintf & fscanf functions, Random access to files and fseek function. C supports a number of functions that have the ability to perform basic file operations, which include:

 
by Dinesh Thakur Category: File Handling

Let us use a while loop in conjunction with the getc macro to read a text file character by character as explained in Program. The counting of characters and lines is very straight forward, the code for which is given below.

 
by Dinesh Thakur Category: File Handling

The fgets function reads a sequence of character, i. e., a character string from an input stream. Its prototype is given below.

 
by Dinesh Thakur Category: File Handling

Function putw()

This function writes an integer (whole number) into a file. The prototype of the function is as shown below.

 
by Dinesh Thakur Category: File Handling

To read a string in a file we can use fgets () whose prototype is:

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

 
by Dinesh Thakur Category: File Handling

The fscanf () function works as scanf (). The difference is that fscanf () reads a file and not the computer keyboard. prototype:

int fscanf (FILE * fp, char * str, ...);



 
by Dinesh Thakur Category: File Handling

The fputc function and putc macro are used to write a character to an output stream. Their prototypes are given below.

 
by Dinesh Thakur Category: File Handling

The devices used with a computer, such as a keyboard, monitor, printer, hard disk, magnetic tape, etc., have widely varying properties regarding data input and output. To simplify data I/O operations, the C standard library supports a simple mode of input and output based on the concept of a stream.



 
by Dinesh Thakur Category: File Handling

Function clearerr ()

The function prototype is as given below.

void(clearerr(FILE* Stream);

 
by Dinesh Thakur Category: File Handling

When I/O operations on a file are complete, we must close the file using the fclose function. The prototype of this function is as follows:

 

Page 1 of 2



Subscribe To Free Daily Newsletter!

Get Free News Updates Delivered Directly To Your Inbox
About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to contact us.



What's New and Popular