File Handling

File Handling:

WHAT IS FILE?

File is a collection of bytes that is stored on secondary storage devices like disk. There are two kinds of files in a system. They are,
1. Text files (ASCII)
2. Binary files
• Text files contain ASCII codes of digits, alphabetic and symbols.
• Binary file contains collection of bytes (0’s and 1’s). Binary files are compiled version of text files.

BASIC FILE OPERATIONS IN C PROGRAMMING:

There are 4 basic operations that can be performed on any files in C programming language. They are,
1. Opening a file
2. Closing a file
3. Reading a file
4. Writing in a file

File operation/Syntax Description
file open FILE *fp1; fp1=fopen(“filename”, ”‘mode”); The fopen function is used to open a file. Where, fp1 is file pointer to the data type “FILE”.
file close: fclose(fp1); fclose function closes the file that is being pointed by file pointer fp1.
file read: fgets(buffer, size, fp1); fgets is used to read a file line by line. where, buffer – buffer to put the data in.size – size of the buffer fp1 – file pointer
file write: fprintf(fp1, “some data”); fprintf(fp1, “text %d”, variable_name); fprintf writes the data into a file pointed by fp1.

 

MODE OF OPERATIONS PERFORMED ON A FILE IN C LANGUAGE:

There are many modes in opening a file. Based on the mode of file, it can be opened for reading or writing or appending the texts. They are listed below.
• r – Opens a file in read mode and sets pointer to the first character in the file. It returns null if file does not exist.
• w – Opens a file in write mode. It returns null if file could not be opened. If file exists, data are overwritten.
• a – Opens a file in append mode. It returns null if file couldn’t be opened.
• r+ – Opens a file for read and write mode and sets pointer to the first character in the file.
• w+ – opens a file for read and write mode and sets pointer to the first character in the file.
• a+ – Opens a file for read and write mode and sets pointer to the first character in the file. But, it can’t modify existing contents.

Example program for file open, file write and file close in c language

# include<stdio.h>

# include<string.h>

int main( )

{

FILE *fp1 ;

char data[50];

// opening an existing file

printf( “Opening the file test.c in mode of write” ) ;

fp1 = fopen(“test.c”, “w”) ;

if ( fp1 == NULL )

{

printf( “Could not open file test.c” ) ;

}

printf( “\n Enter some text from keyboard” \ “ to write in the file test.c” ) ;

// getting input from user

while ( strlen ( gets( data ) ) > 0 )

{

// writing in the file fputs(data, fp1) ;

fputs(“\n”, fp1) ;

}

// closing the file

printf(“Closing the file test.c”) ;

fclose(fp1) ;

}

Output

Opening the file test.c in mode of write

Enter some text from keyboard to write in the file test.c

welcome to prosoftek.

Closing the file test.c

WAP to Open, Read and close a file: reading string by string

# include<stdio.h>

int main( )

{

FILE *fp1 ;

char data[50] ;

printf( “Opening the file test.c in read mode” ) ;

fp1 = fopen( “test.C”, “r” ) ;

if ( fp 1== NULL )

{

printf( “Could not open file test.c” ) ;

}

printf( “Reading the file test.c” ) ;

while( fgets ( data, 50, fp1 ) != NULL )

printf( “%s” , data ) ;

printf(“Closing the file test.c”) ;

fclose(fp1) ;

}

Output

Opening the file test.c in read mode

Reading the file test.c

welcome to prosoftek

Closing the file test.c

Leave a Reply

Your email address will not be published. Required fields are marked *