C Programming | File Handling | Questions
Here are some question and answers on file handling in C language.
File Handling | Questions
Recommended Books for C Programming
To checkout books click on the images below
Question 1
Write a C Program using file pointer to perform the following tasks:
- a. Read a file name from your console.
- b. Read a text from the keyboard until the enter key is pressed and store them into the file.
- c. Open the file and display the file contents on screen.
#include<stdio.h> #include<stdlib.h> int main(int argc, char *argv[]){ FILE *fp; char c[200]; if(argc!=2){ printf("Enter only one file name with extension"); exit(0); } fp = fopen(argv[1],"w"); if(fp==NULL){ printf("Error in creating the file."); exit(0); } printf("Enter the string: "); gets(c); fputs(c,fp); fclose(fp); fp = fopen(argv[1],"r"); printf("String in file: "); fgets(c,200,fp); puts(c); fclose(fp); }
Question 2
Suppose, xyz.c file is already stored in your machine. Write a C program to open the file and count the number of words, characters and sentences present in the file.
#include<stdio.h> #include<stdlib.h> int main(){ FILE *fp; int space=0,total=0,lines=0; char ch; fp = fopen("xyz.txt","r"); if(fp==NULL){ printf("Error in creating the file."); exit(0); } while ((ch=fgetc(fp))!=EOF){ ++total; printf("%c",ch); if(ch==' ') space++; else if(ch=='\n') lines++; } printf("Words: %d, characters: %d and sentences: %d\n",space+lines+1,total-(lines+space),lines+1); }
Question 3
Write a C Program to create a structure employee with five attributes: firstName (10C), lastName(10C), employeeNo (int), age (int) and salary (float). Read a number, say n from the user. Read n records of the type employee from the user and store them in an array. You should dynamically allocate the memory for the array. Thereafter, write the content of the array into a file. Finally, display the data stored in the file.
#include<stdio.h> #include<stdlib.h> typedef struct{ char firstName[10], lastName[10]; int emplyeeNo,age; float salary; }employee; int main(){ FILE *fp; employee *p; int n; printf("Enter no. of employee records: "); scanf("%d",&n); p=(employee *)malloc(n*sizeof(employee)); for(int i=0; i<n; i++){ printf("Enter first name, last name, employee number, age and salary: \n"); scanf("%s%s%d%d%f",p[i].firstName,p[i].lastName,&p[i].emplyeeNo,&p[i].age,&p[i].salary); } fp = fopen("employee.txt","w"); if(fp==NULL){ printf("Error in creating the file."); exit(0); } for(int i=0; i<n; i++){ fprintf(fp,"%s %s %d %d %f\n",p[i].firstName,p[i].lastName,p[i].emplyeeNo,p[i].age,p[i].salary); } fclose(fp); fp = fopen("employee.txt","r"); if(fp==NULL){ printf("Error in opening file."); exit(0); } for(int i=0; i<n; i++){ fscanf(fp,"%s %s %d %d %f\n",p[i].firstName,p[i].lastName,&p[i].emplyeeNo,&p[i].age,&p[i].salary); } for(int i=0; i<n; i++){ printf("Employee %d\nFirst Name:%s\nLast Name:%s\nEmployee Number:%d\nAge:%d\nSalary:%f\n\n",i+1,p[i].firstName,p[i].lastName,p[i].emplyeeNo,p[i].age,p[i].salary); } fclose(fp); }
Question 4
Write a C program to create a new file (with some file name) and enter the text through keyboard. Write appropriate C functions to determine and display the following on the contents of the file:
- a. The longest and the shortest words.
- b. The words with the minimum and maximum number of vowels.
- c. All repeated words.
Example: Suppose, a new file by the name “file.txt” is created and the following text is entered through the keyboard:
Congress MP, Sashi Tharoor used the following words to highlight the dishonesty of a journalist: Exasperating farrago of distortions, misrepresentations and outright lies, being broadcast by an unprincipled showman masquerading as a journalist.
The output of the program on this file for questions a, b and c are as follows:
The longest word is “misrepresentrations”. The shortest word is “a”. The words with the minimum number of vowels are “MP”, “by”. The word with the maximum number of vowels is “misrepresentations”. The repeated words are “the”, “a”.
#include<stdio.h> #include<stdlib.h> #include<string.h> // function to check max and min words void max_min_word(char *line){ // file FILE *fp; fp = fopen("file.txt","w"); if(fp==NULL){ printf("Error in creating the file."); exit(0); } // variables char ch[30],max_word[30],min_word[30]; int max=0,min=200,j=0; // loop to check for(int i=0; line[i]!='\0'; i++){ if(line[i]==' '||line[i]=='.'||line[i]==','||line[i]=='"'||line[i]==':'){ // condition to check at end of word ch[j]='\0'; // max word condition if(strlen(ch)>max){ strcpy(max_word,ch); max=strlen(ch); } // min word condition if(strlen(ch)!=0){ if(strlen(ch)<min){ strcpy(min_word,ch); min=strlen(ch); } } // resetting the extracting variable j=0; } else{ // getting characters ch[j++]=line[i]; } } // writing the data in file fprintf(fp,"The longest word is \"%s\". The shortest word is \"%s\".\n",max_word,min_word); fclose(fp); } // function to check max and min vowel words void max_min_vowels(char *line){ // file FILE *fp; fp = fopen("file.txt","a"); if(fp==NULL){ printf("Error in creating the file."); exit(0); } // variables char ch[30],min_vowel_word[10][30],max_vowel_word[10][30]; int j=0,max_vowel=0,min_vowel=200,vowel=0,m=0,n=0; // loop to check the max and min vowel words for(int i=0; line[i]!='\0'; i++){ if(line[i]==' '||line[i]=='.'||line[i]==','||line[i]==':'){ // condition to check at end of word // max vowel word if(line[i+1]==' ') i++; if(strlen(ch)!=0){ ch[j]='\0'; if(max_vowel==vowel){ // if more than one max vowel word exist strcpy(max_vowel_word[m++],ch); }else if(max_vowel<vowel){ m=0; strcpy(max_vowel_word[m++],ch); max_vowel=vowel; } // min vowel word if (min_vowel==vowel){ // if more than one min vowel word exist strcpy(min_vowel_word[n++],ch); }else if(min_vowel>vowel){ n=0; strcpy(min_vowel_word[n++],ch); min_vowel=vowel; } } // resetting the extracting variables j=0; vowel=0; } else{ // extracting variables ch[j++]=line[i]; // vovel count if(line[i]=='a'||line[i]=='A'||line[i]=='e'||line[i]=='E'||line[i]=='i'||line[i]=='I'||line[i]=='o'||line[i]=='O'||line[i]=='u'||line[i]=='U') vowel++; } } // printing min vowel words in file fprintf(fp,"The words with the minimum number of vowels "); // if more than one words if(n>1) fprintf(fp,"are"); else fprintf(fp,"is"); // min vowels for(int i=0; i<n; i++){ fprintf(fp," \"%s\"",min_vowel_word[i]); } // printing max vowel words in file fprintf(fp,".The word with the maximum number of vowels "); // if more than one words if(m>1) fprintf(fp,"are"); else fprintf(fp,"is"); // max vowels for(int i=0; i<m; i++){ fprintf(fp," \"%s\"",max_vowel_word[i]); } fprintf(fp,".\n"); fclose(fp); } // function to check repeated words void repeated_words( char *line){ // file FILE *fp; fp = fopen("file.txt","a"); if(fp==NULL){ printf("Error in creating the file."); exit(0); } // function variables char ch[30],check[30],words[20][30]; int count=0,j=0,n=0,l,flag=1; // main loop for(int i=0; line[i]!='\0';i++){ if(line[i]==' '||line[i]=='.'||line[i]==','||line[i]==':'){ ch[j]='\0'; if(line[i+1]==' ') i++; if(n!=0&&(strlen(ch)!=0)){ for(int k=0; k<n; k++){ if(strcmpi(ch,words[k])==0){ flag=0; } } } if(flag!=0&&(strlen(ch)!=0)){ count=0; l=0; for(int k=0;line[k]!='\0';k++){ if(line[k]==' '||line[k]=='.'||line[k]==','||line[k]==':'){ check[l]='\0'; if(line[k+1]==' ') k++; if(strlen(check)!=0){ if(strcmpi(ch,check)==0) count++; } l=0; } else check[l++]=line[k]; } if(count>1){ strcpy(words[n++],ch); printf("done"); } } printf("|%s|",ch); j=0; } else ch[j++]=line[i]; } // printing into file fprintf(fp,"The repeated "); if(n>1) fprintf(fp,"words are"); else fprintf(fp,"word is"); for(int i=0; i<n; i++) fprintf(fp," \"%s\"",words[i]); fclose(fp); } int main(){ char line[1000]; printf("Enter text :"); gets(line); max_min_word(line); max_min_vowels(line); repeated_words(line); }
Question 5
Write a C program to create a file and enter some N data items (integer data) in the first attempt. In the next time, open the same file and append say M data items to it. Later, fetch the contents of the file and copy them to three new files such that positive data items will be copied to a file named “positive”, negative items will be copied into a file named “negative” and a third file named “absolute” should contain the absolute values of all the data items in the original file. After the operation, print the contents of original file (initial one) as well as the other three files.
Example: Suppose, a file named “numers.txt” is created and initially, the following N = 6 (taken from the user) integers are entered: 6 -8 7 0 4 -2. Next time, let the file be opened again and the following M = 5 (taken from the user) integers are appended: -5 6 89 -3 -9.
Then, the file named “positive.txt” should contain the following: 6 7 4 89. The file named “negative.txt” should contain the following: -8 -2 -5 -3 -9. The file named “absolute.txt” should contain the following: 6 8 7 0 4 2 5 6 89 3 9. Finally, the original file named “numbers.txt” should contain the following: 6 -8 7 0 4 -2 -5 6 89 -3 -9.
#include<stdio.h> #include<stdlib.h> int main(){ FILE *fp,*pos,*neg,*abso; int i,num[100],n,m,flag=1; char c; pos = fopen("positive.txt","w"); neg = fopen("negative.txt","w"); abso = fopen("absolute.txt","w"); if((fp=fopen("numbers.txt","w"))==NULL){ printf("File cannot be created."); exit(0); } printf("Enter number of numbers you want to enter: "); scanf("%d",&n); printf("Enter: "); for(i=0; i<n; i++){ scanf("%d",&num[i]); fprintf(fp,"%d ",num[i]); } fclose(fp); if((fp=fopen("numbers.txt","a"))==NULL){ printf("File cannot be opened"); exit(1); } printf("Emter number of numbers you want to add: "); scanf("%d",&m); printf("Enter: "); for(i=n; i<m+n; i++){ scanf("%d",&num[i]); fprintf(fp,"%d ",num[i]); } fclose(fp); for(i=0; i<n+m; i++){ if(num[i]>0){ flag=1; for(int j=0; j<i; j++){ if(num[i]==num[j]) flag=0; } if(flag!=0) fprintf(pos,"%d ",num[i]); fprintf(abso,"%d ",num[i]); } else if(num[i]<0){ flag=1; for(int j=0; j<i; j++){ if(num[i]==num[j]) flag=0; } if(flag!=0) fprintf(neg,"%d ",num[i]); fprintf(abso,"%d ",-num[i]); } else fprintf(abso,"%d ",num[i]); } fclose(pos); fclose(neg); fclose(abso); fp = fopen("numbers.txt","r"); pos = fopen("positive.txt","r"); neg = fopen("negative.txt","r"); abso = fopen("absolute.txt","r"); printf("data in original file: "); while((c=getc(fp))!=EOF){ printf("%c",c); } printf("\ndata in positive file: "); while((c=getc(pos))!=EOF){ printf("%c",c); } printf("\ndata in negative file: "); while((c=getc(neg))!=EOF){ printf("%c",c); } printf("\ndata in positive file: "); while((c=getc(abso))!=EOF){ printf("%c",c); } fclose(fp); fclose(pos); fclose(neg); fclose(abso); }
Recommended Books for C Programming
To checkout books click on the images below
good work Vikas, keep it up.
I like this web site very much so much great info .