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

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

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

Function clearerr ()

The function prototype is as given below.

void(clearerr(FILE* Stream);

 
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 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

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 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

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 Category: File Handling

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 Category: File Handling

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

 
by Dinesh Thakur Category: File Handling

The function prototype is written in the following manner:

int ungetc(int c, File *stream);

 
by Dinesh Thakur Category: File Handling

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 Category: File Handling

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

 
by Dinesh Thakur Category: File Handling

The prototype of this function is given below.

int remove(const char* filename);

 
by Dinesh Thakur Category: Puppetting On Strings

sprintf: This Writes formatted data to a character string in memory instead of stdout

 
by Dinesh Thakur Category: Puppetting On Strings

Let us write function str words to accept a string and separate the words in it. The number of words in a given string as well as the number of characters in each word are not known before hand. To keep the code simple, let us assume that the given string contains at the most MAX WORD words and each word in turn contains at the most MAX_ CHAR characters. Thus, the function str words can return the words in the given string in a two-dimensional array of size MAX_ WORD x MAX_CHAR of type char, as shown in fig.

 

Page 15 of 16



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular 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.