Turn Desktop View Off
by Dinesh Thakur

Function clearerr ()

The function prototype is as given below.

void(clearerr(FILE* Stream);

 
by Dinesh Thakur

Function putw()

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

 
by Dinesh Thakur

We have dealt with concepts like how to write and retrieve characters and texts from files. For mixed data types and for formatting, we use functions fprintf () and fscanf (). These functions are similar to functions printf () and scanf () which are used for standard output and input, respectively, except for the fact that the first argument is the FILE* stream, the second argument is the formatting string, and the remaining are the variable names to be printed in file.

 
by Dinesh Thakur

Appending to a file means adding at the end of the file while keeping the previous contents of the file intact. For this the file open mode is "a". Program illustrates appending a file.

 
by Dinesh Thakur

The function prototype is written in the following manner:

int ungetc(int c, File *stream);

 
by Dinesh Thakur

The prototype of this function is given below.

int remove(const char* filename);

 
by Dinesh Thakur

This function is used to change the name of file. Its prototype may be written as given below.

 
by Dinesh Thakur

This function may be used for opening a file in different modes. The function prototype may be written in the following manner:

 
by Dinesh Thakur

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

Let us define a structure student to store the HSC marks data of a student as follows:

 
by Dinesh Thakur

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

If a character read from an input stream is not appropriate for the current operation, we can push it back to the stream using the ungetc function. A subsequent character read operation (such as fgetc or getc) on that stream will return this character. The prototype of the ungetc function is given below.

 
by Dinesh Thakur

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

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

 
by Dinesh Thakur

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

The fgetc function and getc macro read a single character from a specified input stream. Their prototypes are given below.

 
by Dinesh Thakur

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:

 
by Dinesh Thakur

The fopen function is used to open new file (create a file) or open an existing file for I/O operations. Its prototype is given below.

 
by Dinesh Thakur

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

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: