Sie sind auf Seite 1von 26

Files

Types of File I/O

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

that allow us to work with files.


The fopen() is used to open a file.
FILE* fopen(char * filename, char *mode)
FILE is a structure defined in <stdio.h>

Modes
Mode
r
w
a
r+

File does not exist


NULL
Creates new
Creates new
NULL

operations
read
overwrites
appends
Read, write
and change

w+

Creates new

read,
overwrites

a+

Creates new

Read and
append

Unformatted reading and writing functions

int getc(FILE *stream)


int fgetc(FILE *stream)
int fputc(char ch,FILE *stream)
char * fgets(char *s, int maxlen, FILE
*stream)
int fputs(char *s, FILE *stream)
int ungetc(int c, FILE *stream)

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.

Reading and writing unformatted text file


#include<stdio.h>
#include<string.h>
FILE *fp;
main(){
int write();
int read();
write();
Writing lines and reading char by char
read();
}
int write(){
char s[80];
fp=fopen("my.txt","w");
puts("enter a string and 1 to end");
8

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

Formatted reading and writing functions

int fprintf(FILE *fp,char *format[,


arguments]);
int fscanf(FILE *fp,char *format[,
address]);

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

enter name: Reena


enter empno: 456
enter salary: 12000
enter name: 1
Veena 111 12000.00
Anjali 123 15000.00
Reena 456 12000.00
14

Reading and writing records


Reading and writing records implies reading and writing structure
variables.
Here we open the file in binary mode.
Functions:
unsigned fwrite(void *ptr,unsigned size,unsigned
n,FILE *stream)
unsigned fread(void *ptr,unsigned size,unsigned
n,FILE *stream)

where n is number of records

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

Methods to move within the file


int fseek(FILE *stream, long offset, int

fromwhere)
Enumerated values for fromwhere:
SEEK_END
SEEK_CUR
SEEK_SET
int ftell(FILE *stream)
void rewind(FILE *stream)

19

Example demonstrating append, modifying, deleting and display of employee


records

#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;

Read the file record by


record to find the employee
number.

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;

Copy the file, record by


record, to a temp file
excluding the record that you
want to delete.

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

Das könnte Ihnen auch gefallen