C-file-handling .

In this session i have discussed file handling preprocessor in C .


In C program when you run and terminate the program it will lost all the data . If you want to preserve the data in that case you can use the file . File can store the data permanent and you can transfer this data one computer to another .


There are two types of file as follows .

1-Text file

2-Binary file

1-Text file

The text file is simple text file . The extension of text file is .txt . You can create the text file using notepad or any text editor . You can easly read and edit the text file . It is less secure .

2-Binary file

The binary file is stored the data in binary format (0 and 1) . The extension of binary file is .bin . You can not read and edit the content easly . It is secured .


File Operation

In C , you can perform the following operation on file .

1-create a new file .

2-open an existing file .

3-read and write the data to a file .

4-close the file .


Working with files

You have need to declare a pointer of type File . It is the communication medium for file and program .

    File*fptr
    

Open the file write the file code .

    ftpr=fopen("finelname","mode")
    

For example

    fopen("E:\\newfile.txt","w");
    
    fopen("E:\\newfile.txt","r");
    

The first code create the file in E drive and the file name is newfile .

Close the file write the below code .

    fclose(file pointer);
    

For example

    fclose(fptr);
    

Different file mode as below .

File Mode
Meaning of Mode
r
Open the file for reading the data .
rb
Open binary file for reading the data .
w
Open file for writing the data .
wb
Open binary file for writing the data .
a
Open the file for append the data . The data is added at the end of file
ab
Open the binary file for append the data . The data is added at the end of file
r+
Open the file for reading and writing the data both .
rb+
Open the binary file for reading and writing the data both .
w+
Open the file for reading and writing the data both .
wb+
Open the binary file for reading and writing the data both .
a+
Open the file for reading and append the data both .
ab+
Open the binary file for reading and append the data both .
.

Write the text into a file using fprintf() .

fprintf() is used to write the set of characters into a file .

Example : Write a program that write the data into a file using fprintf() .

  #include<stdio.h>
  int main()
  {
     FILE *fp;  
     fp = fopen("E:\\file.txt", "w");//opening file  
     fprintf(fp, "Hello sir how are you ...\n");//writing data into file  
     fclose(fp);//closing file
     printf("Data write into file successfully");   
     return 0;
  }
    
  OUTPUT : Data write into file successfully
    

The above code create the file.txt file in E drive . Open the E drive and check it .


Read the set of characters from the file using fscanf().

fscanf() is used to read the set of characters from the file .

Example Write a program that read the set of characters from the file using fscanf() .

  #include<stdio.h>
  int main()
  {
     FILE *fp;
     char str[150];
     fp = fopen("E:\\file.txt", "r");
     while(fscanf(fp, "%s", str)!=EOF)
     {
        printf("%s ", str);
     }
     fclose(fp);
     return 0;
  }
  
  OUTPUT Hello sir how are you ...
  

Write the character into file using fputc() .

fputc() is used to write a single character into a file .

Example Write a program that write a single character into file using fputc() .

  #include<stdio.h>
  int main()
  {
       char ch;
       FILE *fp;
       fp = fopen("E:\\file.txt", "w");//opening file
       printf("Enter the character press enter for end to write\n");
       while(ch!='\n')
       {
           ch=getchar();
           fputc(ch,fp);
      }
      fclose(fp);
      return 0;
   } 
  OUTPUT : Enter the character press enter for end to write
           Hello sir how are you ...
  

Read the character from file using fgetc() .

fgetc() is used to read a single character from a file .

Example Write a program that read a single character from file using fgetc() .

  #include<stdio.h>
  int main()
  {
        FILE *fp;
        char ch;
        fp=fopen("E:\\file.txt","r");
        while((ch=fgetc(fp))!=EOF)
        {
               printf("%c",ch);
        }
        fclose(fp);
        return 0;
  }
 
  OUTPUT :  Hello sir how are you ...
  

Write the string into file using fputs() .

fputs() is used to write the string into a file .

Example Write a program that write the string into file using fputs() .

  #include<stdio.h>
  int main()
  {
        FILE *fp;  
        fp=fopen("E:\\file.txt","w");  
        fputs("Hello sir how are you",fp);  
        printf("Date write into file successfully");
        fclose(fp);  
        return 0;
  }
 
  OUTPUT :  Date write into file successfully
  

Write the string into file using fgets() .

fgets() is used to write the string into a file .

Example Write a program that write the string into file using fgets() .

  #include<stdio.h>
  int main()
  {
        FILE *fp;  
        char str[150];   
        fp=fopen("E:\\file.txt","r");  
        printf("%s",fgets(str,150,fp));  
        fclose(fp);  
        return 0;
  }
  OUTPUT :  Hello sir how are you 
  

fseek() send the pointer to desired location in file .

fseek() is used to move the pointer to desired location in file .

The fseek() has three values given as follows .

1-SEEK_SET - It is denote the starting of file .

2-SEEK_END - It is denote the end of file

3-SEEK_CUR - It is denote the current location of file .

Example Write the program that send the pointer at desired location in file using fseek().

  #include<stdio.h>
  int main()
  {
         FILE*fp;
         fp = fopen("E:\\file.txt","w+");
         fputs("Rama is a good ", fp);
         fseek( fp, 15, SEEK_SET );
         fputs("boy", fp);
         fclose();  
         return 0;
  }
  OUTPUT :  Ram is a good boy 
  

It will show in file.txt file .


rewind() send the pointer to beginning of file .

rewind() is used to move the pointer to beginning of file .

Example Write the program that send the pointer at beginning of file using rewind().

  #include<stdio.h>
  int main()
  {
      FILE*fp;
      fp=fopen("E:\\file.txt","r");
      rewind(fp);//moves the file pointer at beginning of the file  
      while((c=fgetc(fp))!=EOF)
      {  
            printf("%c",c);  
      }  
      fclose(fp);
      return 0;        
  }
  OUTPUT :  Ram is a good boy 
  

ftell() function is used to get current position of the file pointer.

Example Write a program that demonstrate the use of ftell() .

  #include<stdio.h>
  int main()
  {
        char ch;
        long pos;//char ch;
        FILE *fp;
        fp = fopen("E:\\file.txt","r");
        char str[100];
        while((ch = fgetc(fp))!=EOF)   
        {
              pos=ftell(fp);
              printf("\nPosition of %c is at %ld",ch,pos);
        } 
         fclose(fp); 
         return 0;         
  }
  OUTPUT :   Position of R is at 1
             Position of a is at 2
             Position of m is at 3
             Position of a is at 4
  

Suppose the file contains the Rama .


Welcome to AndroidATPoint.

back image                     next image