Beruflich Dokumente
Kultur Dokumente
File I/O
Binary
Text
unformatted
formatted
unformatted
Fundamental Steps
Open a file
Read or write
Close a file
Opening a file
First step before reading or writing on to a file, is opening
a file.
The <stdio.h> library has all the necessary functions
Modes
Mode
r
w
a
r+
operations
read
overwrites
appends
Read, write
and change
w+
Creates new
read,
overwrites
a+
Creates new
Read and
append
Closing a file
int fclose(FILE *stream)
fclose() does two important things Any characters remaining in the buffer are written to
the disk.
Frees the link between the program and the files so
that the file is available to other programs.
while(1){
gets(s);
if(strcmp(s,"1")==0)
break;
fputs(s,fp);
fputs("\n",fp);}
fclose(fp);
}
int read(){
char ch;
fp=fopen("my.txt","r");
while(1){
ch=getc(fp);
if(ch==EOF)
End of file
break;
else
printf("%c",ch);}
fclose(fp); }
Result of execution:
enter a string and 1 to end
And miles to go
before I sleep
and miles to go before I sleep
1
And miles to go
before I sleep
and miles to go before I sleep
10
11
#include<stdio.h>
#include<string.h>
FILE *fp;
main(){
int write();
int read();
write();
read();
}
int write(){
char name[30];
int empno;
float salary;
fp=fopen("emp.txt","w");
12
while(1){
printf("enter name or enter 1 to stop: ");
scanf("%s",name);
if (strcmp(name,"1")==0) break;
printf("enter empno: ");
scanf("%d",&empno);
printf("enter salary: ");
scanf("%f",&salary);
fprintf(fp,"%s %d
%7.2f\n",name,empno,salary);
fflush(stdin);}
fclose(fp);
Flushes the input stream
}
int read(){
char name[30];
13
int empno;
float salary;
Result:
fp=fopen("emp.txt","r");
while(fscanf(fp,"%s %d %f", enter name: Veena
enter empno: 111
name,&empno,&salary)!=EOF ) enter salary: 12000
printf("%s %d %7.2f \n",
enter name: Anjali
name,empno,salary);
enter empno: 123
enter salary: 15000
fclose(fp);
}
Veena 111 12000.00
Anjali 123 15000.00
Reena 456 12000.00
15
#include<stdio.h>
struct emp{
int num;
char name[30];
double sal;
} e;
FILE *fp;
main(){
int write();
int read();
write();
read();}
int write(){
int c=1;
16
fp=fopen("emp1.dat","wb");
Binary mode
while(c){
printf("enter emp number:");
scanf("%d",&e.num);
printf("enter name:");
sizeof() operator returns
scanf("%s",e.name);
the size of the structure
printf("enter salary:");
scanf("%lf",&e.sal);
fwrite(&e,sizeof(e),1,fp);
fflush(stdin);
printf("enter 0 to stop");
scanf("%d",&c);
}
fclose(fp);
}
17
int read(){
Binary mode
fp=fopen("emp1.dat","rb");
while(fread(&e,sizeof(e),1,fp))
printf("%d %s %f\n",e.num,e.name,e.sal);
fclose(fp);
}
Result of execution:
enter emp number:123
enter name:Jolly
enter salary:3000
enter 0 to stop1
enter emp number:234
enter name:Kate
enter salary:7000
enter 0 to stop0
123 Jolly 3000.000000
234 Kate 7000.000000
18
fromwhere)
Enumerated values for fromwhere:
SEEK_END
SEEK_CUR
SEEK_SET
int ftell(FILE *stream)
void rewind(FILE *stream)
19
#include<stdio.h>
struct emp{
int num;
char name[30];
double sal;
} e;
FILE *fp;
main(){
int i;
int append();
int modify();
int delete();
20
int read();
fp=fopen("e.dat","rb+");
If the file already
exists, open the
if(fp==NULL)
file else create the
fp=fopen("e.dat","wb+");
file.
while(1){
printf("enter \n 1 to append\n 2 to
modify salary\n 3 to delete\n 4 to
print\n 5 to exit\n");
scanf("%d", &i);
fflush(stdin);
switch(i){
case 1: append();
break;
case 2: modify();
break;
21
case 3: delete();
break;
case 4: read();
break;
case 5: fclose(fp);
exit(0);
break;
default: printf("invalid entry");}}
}
int append(){
Go to the end of
fseek(fp,0,SEEK_END);
the file to add.
printf("enter emp number:");
scanf("%d",&e.num);
printf("enter name:");
scanf("%s",e.name);
22
printf("enter salary:");
scanf("%lf",&e.sal);
fwrite(&e,sizeof(e),1,fp);
fflush(stdin);
}
int read(){
rewind(fp);
while(fread(&e,sizeof(e),1,fp))
printf("%d %s %lf\n",e.num,e.name,e.sal);
fflush(stdin);
}
23
int modify(){
int eno;
rewind(fp);
printf("enter emp number to modify:");
scanf("%d",&eno);
while(fread(&e,sizeof(e),1,fp)){
if(e.num==eno){
printf("enter salary\n");
scanf("%lf",&e.sal);
fseek(fp,-sizeof(e),SEEK_CUR);
fwrite(&e,sizeof(e),1,fp);
break;
go back 40 bytes (that is the
size of the employee record)
}
to rewrite the record.
}
}
24
int delete(){
FILE *t;
int eno;
t=fopen("temp.dat","wb");
printf("enter emp number to delete:");
scanf("%d",&eno);
rewind(fp);
while(fread(&e,sizeof(e),1,fp)){
if(e.num!=eno){
fwrite(&e,sizeof(e),1,t);
}}
25
fclose(t);
fclose(fp);
remove("e.dat");
rename("temp.dat","e.dat");
fp=fopen("e.dat","rb+");
}
Remove the old employee file and
rename the temp file with the old emp
file name. Doing this requires you to
close the file stream.
26