FILE HANDLING

What is file handling in C?

  • The file is a sequence of bytes which is used to store data it is a logical entity which will be stored in the memory waving particular address.there are various types of file like “txt”, binary files stored with the extension “bin” etc.
  • Hence in every program, we write in this section to access the file, we use this kind of pointer declaration. This pointer is used to point the declaration
  • This pointer is used to point the data to be accessed in the file
  • For file handling or accessing the contents of the file, there are certain predefined functions available in the C programming language
  • A file represents a sequence of bytes on the disk where a group of related data is stored
  • The file is where a group of related data is stored
  • The file is created for permanent storage of data
  • C programming language can handle files as Stream-oriented data (Text) files and System oriented data Binary files
  • An important thing required to access files is the “FILE pointer”.This pointer is used to point to the values stored in the file
  • A file pointer is hence to be created for accessing the files. The syntax for creating a file pointer is as given below
FILE*<identifier for pointer>;
for eg:FILE *fp;

Some Action Perform in FILE HANDLING

mode in file handling
mode in file handling

(i) “r”: indicates that the file is to be opened indicates in the read mode

(ii) “w”: indicates that the file is to be opened indicates in the write mode. When a file is opened in write mode the data written in the file overwrites the previously stored data in the file

(iii) “a”: indicates that the file is to be opened in the append mode. In this mode, the data written into the file is appended towards the ends of the already stored data in the file. The earlier stored data remains as it is.

(iv) “w+”: indicates that the file is to be opened in write and read mode

(v) “r+”: indicates that the file is to be opened in read and write mode

Hence in every program, we write in this section to access file, we use this kind of pointer declaration. This pointer is used to point the declaration. This pointer is used to point the data to be accessed in the file.

Function of file
Function of file in C

1. fopen() :

  • This function is used to open a file to be accessed in the program
  • The file to be opened is to be passed as a string parameter to the function and also the mode of opening the file is to be passed as the string to the function
  • Hence the syntax of the function with parameters is as given below: <file pointer identifier> = fopen(“<file name>”,”<mode of opening the file>”)
For e.g. fp=fopen("test.txt","w");
  • This example statement opens the file “test.txt” in write mode and the pointer used along with the function to read/write is the file pointer “fp”.

2. fclose() :

  • This function is used to close the file opened using the file pointer passed to the function. The syntax with parameters to call this function is as given below: fclose(<file pointer identifier>);
For example fclose(fp);
  • This statement closes the file opened using the file pointer variable “fp”.It closes the file opened using the function open(). The file opened to be accessed in whatsoever mode doesn’t matter, it is closed using the fclose() function.

3. feof() :

  • This function returns true or false based on whether the pointer pointing to the file has reached the end of the file or not
  • The pointer used to point the file has to be passed as a parameter to the function feof().
  • Also, the file has to be opened pointed using the pointer “fp” , before using this function
  • The syntax of the function with the parameters is as shown below: feof(<file pointer identifier>);
For example feof(fp);

4. fputc() :

  • This function is used to put a character type data into the opened file using the fopen() function, pointed by a file pointer
  • The character to be put into the file, as well as the file pointer, is to be passed as the parameters to this function
  • The syntax to call this function along with the parameters to be passed is as shown below: fputc(<char type data>,<file pointer identifier>);
For example:
fputc(c,fp);
  • This example will store the character value of the char type data variable “c” into the opened file and pointed by the pointer fp in the file

5. getc() :

  • This function is used to get a character from the file pointed by the corresponding file pointer passed to the function
  • It is exactly opposite the fputc() function
  • This function brings the character from the file opened and pointed by the file pointer variable passed to the function
  • The syntax of the function call with the parameters to be passed is as given below: getc(<file pointer identifier>);
For example: getc(fp);
  • This function brings the character type data from the opened file using the pointer fp; from the location pointed by the pointer “fp” in that file

6. rewind() :

  • This function is used to rewind or bring the file pointer variable to point to the beginning of the file from wherever it is currently pointing in the file
  • The syntax of the function call with the parameters to be passed is as given below: rewind(<file pointer identifier>);
For example : rewind(fp);
  • This function rewinds or brings back the pointer “fp” to the beginning of the file from wherever it was pointing in the file opened using the pointer “fp”

7. fprintf():

  • This function is used to store the different data types in the file as the fputc() function is used to store the character in the file
  • This can be used to store integer, float, string etc types of data into the file opened
  • The function is similar to printf() , except that it writes to the file instead of the monitor
  • The other difference is in the syntax that the file pointer variable is also to be passed to the function along with the data types and the parameter values or the variables
  • The syntax shown below explains the concept : fprintf(<file pointer identifier>,”<format specifiers>”,<variable names>);
For example : fprintf(fp"%d",x);
  • This function will print or store the value of the integer variable “x” in the file opened using the pointer “fp”
  • The data will be stored at the location pointed by the pointer variable “fp”

8.fscanf() :

  • The function is used to read the different types of data as the getc() function is used to read a character from the file. This function can be used to read an integer, float string etc types of data into the file opened
  • The function is similar to the file opened. the function is similar to scanf(), except that it reads from the file instead of the keyboard
  • The other difference is in the syntax, that the file pointer variable is also to be passed to the function along with the data types and the parameters values or the variables
  • The syntax shown below explains the concept: fscanf(<file pointer variable>,”<format specifiers>”,<address of the variables in which the data is to be read>);
For example : fscanf(fp,"%d",&x);
  • This function will read the integer type data from the file pointed by the file pointer variable “fp” and store this value I the variable “x”
  • The value will be brought from the file at the position pointed by the file pointer in the file

1.Display the program to accept a set of character from the user until the user presses the full stop(.) and store it in the text file

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char c;
clrscr();
fp=fopen("test.txt","w");
printf("write the data to store in the file :\n");
while(c!='.')
{
scanf("%c",&c);
fputc(c,fp);
}
fclose(fp);
fp=fopen("test.txt","r");
while(!feof(fp))
{
printf("%c",getc(fp));
}
fclose(fp);
getch();
}  

2.Example File Handling (Reading File)


#include<stdio.h>  
#include<conio.h>  
void main(){  
FILE *fp;  
char text[300];  
clrscr();  
fp=fopen("myfile2.txt","r");  
printf("%s",fgets(text,200,fp));  
  
fclose(fp);  
getch();  
}  


Output:
hello c programming

Translate »