Home » Posts tagged 'file-handling'

# Tag Archives: file-handling

## File handling programs in C Programming

Example : Write a C program to copy the contents of one file to another file.

OUTPUT

Example : Write a C program to appends the content of file at the end of another file.

Or

Example : Write a C program to merge two files into third file.

Example: Write a C program to count spaces ,vowels, consonants, digits in a file .

## Writing to a File in C Programming

To write on a file there are following  function available:

1.  Function fputc() : function fputc() write individual characters at a time to the file. The syntax of this function is –

Syntax :

int  fputc( int c,  FILE *fp );

Function fputc()  returns the written character written on success and  if there is an error it will return EOF (Emd of File).

• Function fputs() :  function fputs() writes the string s’ to the output stream referenced by fp. The syntax of this function is –

Syntax :

int fputs( const char *s, FILE *fp );

Function fputs()  returns the non-negative value  on success and  if there is an error it will return EOF (Emd of File).

• Function fprintf(): Function fprintf()write a string into a file. The syntax of this function is –

Syntax :

int  fprintf(FILE  *fp,  const  char *format, …)

Example: Write a C program to write a single character into file using fputc() function in file handling.

OUTPUT

a
BOOK1.txt

Example: Write a C program to write a string into a file using fputs() and fprintf() function in file handling.

OUTPUT

Hello  hii  how are you

Book1.txt

Description: When  we run the above program , it creates a new file Book1.txt in /c directory and writes two lines using fputs() and fprintf() functions.

Example : Write a C program to open a file, write in it and close the file.

OUTPUT

i am aditya from it department

Book1.txt

## Reading a File in C Programming

To read a  file there are following  function available:

1. Function fgetc():  Function fgetc() is used to read a single character at a time from the input file. The syntax of this function is −

Syntax :

int fgetc( FILE * fp );

• Functions fgets():  Functions fgets() is used to reads string from input file, one line at a time. The syntax of this function is –

Syntax :

char *fgets( char *buf, int n, FILE *fp );

function fgets() copies the read string into the buffer buf and appending a null character to terminate the string.

Here “n” is a size of buffer.

*fp- is a file pointer.

If the function fgets() encounters a character ‘\n’  ( newline ) or the end of the file (            EOF ) before reading the maximum number of characters, then it will returns the characters read up to that point including the new line character.

• Function fscan(): Function fscan() is also use to read strings from a file. The syntax of this function is –

Syntax :

int fscanf(FILE *fp, const char *format, …)

Note: fscan() function stops reading after encountering the first space character in file.

## Closing a File

The fclose() function is used to close an already opened file.  The syntax of this function is −

Syntax :

int   fclose( FILE *fp );

Function fclose(-) returns zero if the file is successfully closed, or EOF if there is an error in closing the file.

Example:  Write a C program to show the use of fgets() and fscan() function using file handling.

Suppose we have a file Book.txt, that have following content

 Hello  hii  how are you I am aditya

OUTPUT

Description:  fscanf() function read only “Hello” because after that, it encountered a space. fscan() function stops reading after encountering the first space character in file.

Function fgets() reads the remaining line till it encountered end of line. fgets() is  reads string from input file, one line at a time.

Finally, the last fgets() reads the second line completely.

Example: Write a C program to open a file, read a file and close file using file handling.

Or

Example:  Write a program to show the use of  fgetc() function.

Note:  Used for  reads a single character from file. It returns EOF at the end of file.

OUTPUT

The content of the file “file1” will be printed.

## File Handling in C Programming

A file represents a sequence of bytes and these file’s is used to store information. There are various different operations that we can be performed on a file are:

1. Creation of a new file (fopen with attributes as “a” or “a+” or “w” or “w++”)
2. Opening an existing file (fopen)
3. Reading from file (fscanf or fgetc)
4. Writing to a file (filePointerrintf or filePointeruts)
5. Moving to a specific location in a file (fseek, rewind)
6. Closing a file (fclose)

## Functions for file handling

There are various predefined functions available for handle file. Some of them mentioned below:

Function Description
fopen() Used for opens new or existing file
fprintf() Used for write data into the file
fscanf() Used for reads data from the file
fputc() Used for writes a single character into the file
fgetc() Used for  reads a single character from file
fclose() Used for closes the file
fseek() sets the file pointer to given position
fputw() Used for writes an integer to file
fgetw() Used for  reads an integer from file
ftell() returns current position
rewind() sets the file pointer to the beginning of the file

## Create or Open Files

We can create a new file or open an existing file by using fopen() function. To create a new file or open an existing file syntax is:

FILE   *fopen( const char * filename, const char * mode );

Here, filename is a string literal and use one of the following modes in the fopen() function if you are working with text file:-

File Mode Meaning of Mode
r opens a file in read mode. If the file does not exist, fopen() returns NULL.
w opens a file in write mode. If the file does not exist, fopen() returns NULL.
a opens a file in append mode ( Data is added to end of file). f the file does not exists, it will be created.
r+ opens a file in a both read and write mode. If the file does not exist, fopen() returns NULL.
w+ opens a file in both read and write mode. If the file exists, its contents are overwritten. If the file does not exist, it will be created.
a+ opens a file in both read and write mode. If the file does not exists, it will be created.
Managed by MochaHost - Fast & Easy WordPress Hosting