Search

File Management

 File Management  using C

Dear Learner,

You will get interesting file management program on this page. We create a File for permanent storage of data. C language provides many functions that help us to perform various file operations. Some of the file management functions are given below:


Function Name
Description
fopen()
To create a new file or for opening existing file
fclose()
To close a file
fgetc()
To read a character from a file
fputc()
To write a character to a file
fscanf()
Reads data from a file
fprintf()
Writes data to a file

We use a structure pointer of file type as shown below in C language:

  FILE *fp;

General Syntax to open a file is as under:

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

Where, filename is the name of the file to be opened and mode specifies the purpose of opening the file. The important modes to operate a file are as under:


Mode
Description
r
opens a file for reading
w
opens or create a text file for writing
a
opens a text file for appending content
r+
opens a text file in both reading and writing mode
w+
opens a text file in both reading and writing mode
a+
opens a text file in both reading and writing mode





Sample C File Management Programs



C Program to read one character from a test.txt file.

#include<stdio.h>
int main(){

 FILE *fp;
 char ch;

 //code to open file for reading
 fp=fopen("test.txt","r");
 
 if(fp==NULL){
     printf("File error");
     exit(0);
 }

 //code to read a character
 ch fgetc(fp);
  
 printf("%c",ch);
 fclose(fp);
 
 return 0;
}




#include<stdio.h>
int main(){

 FILE *fp;
 char ch;

 fp=fopen("test.txt","r");
 
 if(fp==NULL){
printf("File error");
exit(0);
 }
 
 ch fgetc(fp);

 while(ch != EOF){
printf("%c",ch);  
ch=fgetc(fp);
 }
 fclose(fp);
 
 return 0;
}


C Programming File Management Programs


Execute following C programs to learn more file management related functionalities.



1.C program to read one line from a file.


This program will read and print one line from a "test.txt" file.   


#include<stdio.h>
int main(){
 
 FILE *f1, *fopen();
 char str1[80];
 
 f1 = fopen("test.txt","r");
 //Following line will read one line from a text.txt file. 
 fgets(str1,80,f1);
 puts(str1);  

 fclose(f1);
 return 0;
}



2.C program to write a word in text file.


#include <stdio.h>
#include <string.h>
int main()
{
FILE *fp;
char str[80];
fp = fopen("test.txt", "w+");
printf("Enter your message:");
gets(str);
fprintf(fp, "%s",str);
printf("Your message is written in test.txt file.");
fclose(fp);
//File validation is to be performed...
   return 0;
}


Output of program

Enter your message:hello student
Your message is written in test.txt file.



3.C program to copy content of one file

 to other.


#include<stdio.h>
int main() {
   FILE *fp1, *fp2;
   char a;

   fp1 = fopen("test.txt", "r");
   if (fp1 == NULL) {
      puts("cannot open test.txt file.");
      exit(1);
   }

   fp2 = fopen("test1.txt", "w");
   if (fp2 == NULL) {
      puts("test1.txt file error.");
      fclose(fp1);
      exit(1);
   }

   do {
      a = fgetc(fp1);
      fputc(a, fp2);
   } while (a != EOF);
  printf("test.txt is successfully copied to test1.txt.");
  fclose(fp1);
  fclose(fp2);
  return 0;
}

Output of the program

test.txt is successfully copied to test1.txt.


4.C program to read file line by line.

This program will read and print content of the "test.txt" file. 

Note: Save source code (.c) file and "test.txt" file in same folder.

#include<stdio.h> 
int main()
{
 FILE *f1, *fopen();
 char str1[80];
 
 f1 = fopen("test.txt","r");

 while(fgets(str1,80,f1)!=NULL)
 {
  printf("%s",str1); 
 }
 fclose(f1);
 return 0;
}



5.C program to append data to text file.

This C program will add message entered by user into the data.txt file. Note that, present content will remain as it is, and new content will be appended at the bottom of data.

#include <stdio.h>
#include <string.h>
int main()
{
   FILE *fp;
   char str[80];
 
   fp = fopen("data.txt", "a");
 
   printf("Enter your message:");
   gets(str);
 
   fprintf(fp, "%s",str);
 
   printf("Your message is appended in data.txt file.");
   fclose(fp);
   //File validation is to be added..
   return 0;
}


6.Count vowels in given file.



C Program to count Vowels in a given file.


This program will read input from "data.txt" file and prints total number of vowels.


#include<stdio.h>
int main()
{
  FILE *fp;
  char ch;
  int cnt=0;

  fp = fopen("data.txt","r");

  if (fp == NULL)
  {
   printf("Can't Open File.");
   exit(1);
  }
  printf("File content is : ");
  ch = fgetc(fp);
  
  while(ch != EOF)
  {
    putchar(ch);
if( ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
        cnt++;
    ch = fgetc(fp);
  }

  printf("\n\nTotal vowels in given file = %d",cnt);
  return 0;
}

Output of Program

File content is : THIS IS TOP C PROGRAMMING PRACTICALS BLOG.

Total vowels in given file = 10


7.Write a student data into a file using the 

C Structure


FILE MANAGEMENT USING C STRUCTURE


This C Program will write a student data 
into a file using the Structure and File Management
concept of C programming.

Source Code

#include <stdio.h>
//Structure to store student data.
struct student
{
   char fname[30];
   char lname[30];
   int  rollno;
   float percentage;
};
void main ()
{
   FILE *fp;
   //declare structure(student) variable
   struct student input;
   // open student file for writing
   fp = fopen ("student.dat","w");
   if (fp == NULL){
      printf("\nFile opening error..\n\n");
      exit (1);
     }
   printf("Enter \"exit\" as First Name to stop reading user input.");
   while (1)
     {
      printf("\nFirst Name: ");
      scanf ("%s", input.fname);
     
      if (strcmp(input.fname, "exit") == 0)
         exit(1);
     
      printf("Last Name : ");
      scanf ("%s", input.lname);
      printf("Roll Number  : ");
      scanf ("%d", &input.rollno);
      printf("Percentage : ");
      scanf ("%f", &input.percentage);
      // write student data to student.dat  file
      fwrite (&input, sizeof(struct student), 1, fp);
     }
}


Output of program 

Enter "exit" as First Name to stop reading user input.
First Name: K
Last Name : Patel
Roll Number  : 101
Percentage : 90.50

First Name: ABC
Last Name : Shah
Roll Number  : 102
Percentage : 95.50

First Name: exit








No comments:

Post a Comment