| |
FILES in 'C' language
Files are used to store the information on the hard disk permanently. We have a number of file functions available. For creating a file we require a pointer also, which is of FILE type. We have to include the stdio.h to make use of files. We can create, open and close the files. We can also read, write and append the contents of a file. The following are the functions, which are related to files.
1) fopen():- This function is used to open an existing file or to create a new file. It takes 2 parameters. One is the name of the file in double quotes and other is the mode of opening that file. This function returns a value and it is the starting address of the file and is stored in the file pointer.
Syntx:- file pointer = fopen("filename","mode");
2) fclose():- This function is used to close the opened file. It takes only one parameter and it is the file pointer.
Syntax:- fclose(file pointer);
3) fgetc():- This function is used to get a character from the file. It takes one parameter and it is the file pointer.
Syntax: character varaible=fgetc(fp);
4) fputc():- This is used to put a character into the file. It takes 2 parameters. One is the character and the other is the file pointer.
Syntax:- fputc(character, file pointer);
5) getw():- It is used to get an integer value from the file. It takes only one parameter and it is the file pointer.
6) putw():- It is used to store the integer value into the file. When you give any digit number, it is taken as a single value and it is stored in the value. Where as in case of fgetc, that same number is stored as different digits.
Different modes of files :- The file can be open in 3 modes.
They are read ( r), write (w) and append(a ).
In read mode, we can read the information present in the file. For this the file should be present already in the system. In write mode, we can write the information into the file. In this mode, the if a file is already present, it overwrites the contents of the file, and otherwise it creates a new file. In append mode, you can read as well as write the contents into the file. While writing the file contents will be not be overwritten and they are added at the end of the contents of the file.
There are also other types of modes which are formed by adding a '+' before the above modes. They are r+, w+ and a+. The r+ allows us to read and write the information of the file. While writing, it will not overwrite the contents of the file. The w+ mode allows us to write and read the information of the file. While writing it will overwrite the contents of the file. The a+ allows us to read, write and append the information of the file. The following examples illustrates the use of the above all modes.
Eg1: /*to read and write the information of a file*/
#include
main(){
FILE fp;
char ch ;
clrscr();
fp=fopen("hello","w");
while((ch=getche())!=EOF)
fputc(ch, fp);
fclose(fp);
fopen("hello","r");
while((ch=fgetc(fp))!=EOF)
printf("%c", ch);
fclose(fp);
getch();
}
You can avoid a file being overwritten by checking whether a file pointer is NULL or not. You can also accept the file name from the keyboard during run time also. These 2 possibilities are shown in the example below.
Eg2: /*to check a file for existence */
#include
main(){
FILE fp;
char ch, name[10] ;
clrscr();
printf("\nEnter a file name :");
scanf("%s",name);
fp=fopen(name,"w");
if(fp!=NULL)
{
while((ch=getche())!=EOF)
fputc(ch, fp);
fclose(fp);
}
else
printf("\nFile is already present ");
fclose(fp);
getch();
}
Using the above functions, we can add the text to the file. If you want to store data in the form of records, then we have other functions.
1) fscanf():- This function is used to retrieve the whole record data from file at a time. It takes 3 parameters. One is the file pointer, second the "control string" and the third the variables. The syntax is shown below.
Syntax:- fscanf(file pointer, "control string", arguments);
2) fprintf():- This function is used to store the whole record information into a file at a time.
Syntax :- fprintf(file pointer, "control string", arguments);
3) ftell():- It is used to show the position of the file pointer in the file. It gives the no. of bytes occupied till the file pointer position. It takes one parameter and it is the file pointer name.
Syntax: ftell(file pointer);
4) frewind():- It is used to position the file pointer at the starting of the file. It takes one parameter and it is the file pointer.
Sytntax:- frewind (file pointer);
5) fseek():-It is used to position the file pointer at the desired location in the file and to retrieve the required number of characters from the file.
Syntax:- fseek(file pointer, offset, position)
Offset is the number of characters we want to retrieve from the required position. Position indicates the position of the file pointer in the file from where the characters are to be retrieved. Either 0,1 or 2 may indicate the position. '0' indicates starting of the file, 1 indicates the current position and 2 indicate from the end of file. When we are using 2, we have to specify the offset in negative number. It means it will retrieve the character from the end of file in reverse direction(backward).
6) fread():- It is used to retrieve the whole record information at a time. It takes 4 parameters. One is the address of the structure, second the size of the structure, third the number of records and fourth is the file pointer.
Syntax:- fread(address of structure, size of(structure), 1, file pointer);
7) fwrite():- It is used to write the whole record information at a time. It takes 4 parameters. One is the address of the structure, second the size of the structure, third the number of records and fourth is the file pointer.
Syntax:- fwrite(address of structure, size of(structure), 1, file pointer);
When using fread and fwrite the file should be opened in binary format by placing a 'b' after the mode like, rb, wb. In these files the information is stored in binary format which the user cannot understand. But while retrieving using fread we can get the exact data.
Eg1 : /* to create a file for reading, writing and appending */
#include
main(){
FILE fp; char ch ;
clrscr();
fp=fopen("hello","a+");
while((ch=getche())!=EOF)
fputc(ch, fp);
frewind(fp);
while((ch=fgetc(fp))!=EOF)
printf("%c - %u", ch, ftell(fp));
fclose(fp);
getch();
}
Eg2: /* usage of fseek */
#include
main(){
FILE fp;
char ch ;
clrscr();
fp=fopen("hello","r");
fseek(fp,0,5);
ch=fgetc(fp);
printf("%c", ch);
fseek(fp,1,-3);
ch=fgetc(fp);
printf("%c", ch);
fseek(fp,-6,2);
ch=fgetc(fp);
printf("%c", ch);
fclose(fp); getch();
}
Eg3: /* to create a file for storing the records */
#include
struct student {
int no,marks;
char name[10];
};
main() {
FILE *fp,*fr;
char ch='y';
struct student s;
int n;
clrscr();
fp=fopen("stud","w");
while(ch=='y'|| ch=='Y') {
scanf("%d%s%d",&s.no,s.name,&s.marks);
fprintf(fp,"\n%d %s %d", s.no,s.name,s.marks);
printf("\nDo you want to continue?"); ch=getche();
}
fclose(fp);
getch();
}
Eg4: /*removing the required record from the file*/
#include
struct student {
int no,marks;
char name[10];
};
main(){
FILE *fp;
char ch='y';
struct student s;
int n;
clrscr();
fp=fopen("stud","r+");
fr=fopen("stud1","w");
printf("\nEnter rno you want:");
scanf("%d",&n);
while(!feof(fp)) {
fscanf(fp,"%d%s%d",&s.no,s.name,&s.marks);
if(s.no!=n)
fprintf(fr,"\n%d %s %d", s.no,s.name,s.marks);
}
fclose(fp);
fclose(fr);
remove("stud");
rename("stud1","stud");
getch();
}
Eg5: /*creating a file and count no. of lines,words space */
#include
main() {
int cw=0,cl=0,cws=0;
char c,name[10];
FILE *fp;
clrscr();
printf("\nEnter file name");
scanf("%s",name);
fp=fopen(name,"w");
while((c=getchar())!=EOF)
putc(c,fp);
fclose(fp);
fp=fopen(name,"r");
while((c=getc(fp))!=EOF) {
if(c=='\n')
cl++;
if(c==' ') {
cw++;
cws++;
}
}
printf("\n the num of words are %d",cw); printf("\nthe num of lines are %d",cl);
printf("\n the num of white spaces are %d",cws);
fclose(fp);
getch();
}
Eg6: /*Creating and reading a binary file using fread and fwrite */
#include
struct student {
int no,marks;
char name[10];
};
main(){
FILE *fp,*fr;
char ch='y';
struct student s;
clrscr();
fp=fopen("stud","ab");
while(ch=='y'|| ch=='Y') {
scanf("%d%s%d",&s.no,s.name,&s.marks);
fwrite(&s,sizeof(s),1,fp);
printf("\nDo you want to continue?");
ch=getche();
}
fclose(fp);
fp=fopen("stud","rb");
while(!feof(fp)) {
fread(&s,sizeof(s),1,fp);
printf("\n %d %s %d ",s.no,s.name,s.marks);
}
fclose(fp);
getch();
}
Please send comments to vgdarur.javafive@blogger.com
|