4.5 Working with Files Example 1: Write a ‘C’ program to create a data file “FILE1.
TXT” to write or store
File: file is the collection of related information stored on disk with unique name. “456” as integer and also read & display integer stored on it.
#include<stdio.h>
Elements of file handling: int main() [Link]
# Declaration of file pointer: { 456
int num=456,num2;
Syntax: FILE *y;
FILE *pointer; e.g. FILE *fp; y=fopen("[Link]","w");
putw(num, y);
# Opening a File: fclose(y);
fopen () : It is used to open data for writing and reading operations. y=fopen("[Link]","r");
Syntax: num2=getw(y);
Pointer name = fopen(“filename”, “mode”); printf("the number saved in file is : %d",num2);
# Closing File fclose(y);
fclose( ) : It is used to close the data file after reading and wring return 0;
operations. }
c) fputc () : It is used to write single character to data file at a time.
# Modes of opening a file: Syntax:
w : Used to open data File for writing data items.
fputc (string_variable,file_pointer);
r : Used to open data File for reading data items.
d) fgetc ( ) : it is used to read a single character from data file.
a : Used to open existing data File for adding data items.
r+ : Used to open exiting data File for both reading and writing data. Syntax:
w+ : Used to open data file for both writing and reading data items. fgetc (string_variable,file_pointer);
a+ : Used to open exiting data File for both writing and reading. Example2 : Write a ‘C’ program to create a data file “[Link]” to write or store
# Some library functions used for file handling. ‘D’ as single character and also read & display character stored on it.
For writing data items to data file : - #include<stdio.h>
a) putw () b) fputc() c) fputs () d) fprintf () f) fwrite (). int main()
For reading data items from data file :- {
a) getw ( ) b) fgetc () c) fgets () d) fscanf () g) fread () char ch1='D',ch2;
Opening data file for writing, reading and appending operating. FILE *fp;
fp=fopen("[Link]","w");
1) writing /reading integer to and from data file. fputc(ch1,fp);
a) putw () : - It is used to write an integer to data file. fclose(fp);
Syntax: fp=fopen("[Link]","w");
putw (integer, file pointer); ch2=getc(fp);
printf("the character saved is : %c",ch2);
b) getw () :- It is used to read the data items from data file. fclose(fp);
Syntax : return 0;
getw(file pointer); }
e) fputs ( ) :- It is used to write or store string to data file: #include<stdio.h>
syntax: int main()
fputs (string_variable,file_pointer); {
FILE *fp;
char fname[10], lname[10];
f) fgets( ) :- It is used to read the string from data file.
int age;
syntax: fp=fopen("[Link]","w");
fgets (string_variable,file_pointer); printf("Enter first name”);
scanf("%s”, fname);
Example3 : Write a ‘C’ program to create a data file “[Link]” to write or store printf("Enter last name”);
any entered word as string and also read & display string stored on it. scanf("%s”, lname);
#include<stdio.h> printf("Enter age”);
int main() scanf("%d”, &age);
Output fprintf(fp,"%s\t%s\%d”,fname,lname,age);
{
fclose(fp);
char text[100];
return 0;
int j=0; }
FILE *p;
printf("enter any word ");
scanf("%s", text);
Example 5: Write a c program read the data items from “[Link]” which
p=fopen("[Link]","w+");
contains first name ,last name and age of people:
fputs(text,p);
#include<stdio.h>
rewind(p); // reset the pointer back to beginning of data file.
fgets(text,10,p); int main()
printf("%s",text); {
fclose(p); FILE *fp;
return 0; char fname[10], lname[10];
} int age;
fp=fopen("[Link]","r");
g) fprintf( ) : It is used to write formatted character, string or numeric data into a data file. fscanf(fp,"%s%s%d",&fname,&lname,&age);
Syntax: printf("First name : %s\n",fname);
fprinf(filepointer, “format specifier”, variable_list); printf("Last name : %s\n",lname);
printf("Age : %d",age);
h) fscanf( ) : It is used to read formatted character, string or numeric data from a fclose(fp);
data file. return 0;
Syntax: }
fscanf(filepointer, “data format specifier”, &variable_list);
Example 4: Write a c program to create data file “[Link]” and store first name
,last name and age of people:
Example 6 : Write a c program to create a data file “[Link]” to store or write OR
name , post and salary of any 5 employees #include<stdio.h>
int main()
#include<stdio.h> {
int main() clrscr();
{ FILE *fp;
FILE *fp; char name[20],post[20];
char name[20],post[20]; int salary;
int salary ,j; fp=fopen("[Link]","r");
fp=fopen("[Link]","w"); while(fscanf(fp,"%s\t %s\t %d",&name,&post,&salary)!=EOF)
for( j=0;j<5;j=j+1) {
{
printf("Enter name of employee”); printf("\n %s\t %s \t%d",name,post,salary);
scanf("%s”,name);
printf("Enter post of employee”); }
scanf("%s”,post); fclose(fp);
printf("Enter salary of employee”); return 0;
scanf("%s”,&salary); }
fprintf(fp,"%s \t %s \t %d \n",name,post,salary); Example 8: Write a c program to create a data file “[Link]” to store name,sex and age
} of people. The program allows to enter data records of employees as long as the user wants.
fclose(fp); #include<stdio.h>
return 0; #include<conio.h>
} int main()
Example 7: Write a c program to read data items (content) from a data file {
“[Link]” which contains name,post,salary of 5 employees. FILE *fp;
char name[20],sex[20],choice;
#include<stdio.h> int age;
int main() fp=fopen("[Link]","w");
{ do
clrscr(); {
printf("\n Enter name : ");
FILE *fp;
scanf("%s",name);
char name[20],post[20]; printf("Enter sex : ");
int salary,j; scanf("%s",name);
fp=fopen("[Link]","r"); printf("Enter age : ");
for(j=1;j<=5;j=j+1) scanf("%d",&age);
{ fprintf(fp,"\n %s \t %s \t %d",name,sex,age);
fscanf(fp,"%s %s %d",&name,&post,&salary) printf("Do you want to continue (y/n) : ");
printf("\n %s\t %s \t%d",name,post,salary); choice=getch();
} } while (choice =='Y' || choice =='y');
fclose(fp);
return 0; }
return 0; }
Example 8: Write a c program to store name, sex and age of people in exiting data file #include<stdio.h>
“[Link]” and also read and display all the data records of this file. #include<conio.h>
#include<stdio.h> int main()
#include<conio.h> {
int main() FILE *fp;
{ char fname[10], lname[10],ch;
FILE *fp; int emp_id,salary,record=0;
char fname[10], lname[10],ch; fp=fopen("[Link]","r");
int age; while(fscanf(fp,"%d %s%s %d",&emp_id,&fname,&lname,&salary)!=EOF)
fp=fopen("[Link]","a"); {
do If(salary>35000)
{ {
printf("Enter first name : "); printf(“Employee Id : %d\n”,emp_id);
scanf("%s", fname); printf("First name : %s\n",fname);
printf("Enter last name : "); printf("Last name : %s\n",lname);
scanf("%s", lname); printf("Age : %d\n",age);
printf("Enter age : "); printf("---------------------\n");
scanf("%d", &age); }
fprintf(fp,"%s\t%s\t%d\n",fname,lname,age); record=record+1;
printf("do you want to continue(y/n) ");
ch=getch(); }
} while(ch=='y'|| ch=='Y'); printf(“\n total number of records : %d”,record);
fclose(fp); fclose(fp);
fp=fopen("[Link]","r"); return 0;
while(fscanf(fp,"%s%s%d",&fname,&lname,&age)!=EOF) }
{
printf("First name : %s\n",fname);
printf("Last name : %s\n",lname);
printf("Age : %d\n",age);
printf("---------------------\n");
}
fclose(fp);
return 0;
}
Example 9: Write a ‘C’ program to read and display data records from data file “[Link]”
whose salary is more than 35000 and also display the total number of data records in this file.