FILE HANDLING
10.1.1 File Pointers
It is not enough to just display the data on the screen. We need to save it because memory is volatile and its contents would be lost once program terminated, so if we need some data again there are two ways one is retype via keyboard to assign it to particular variable, and other is regenerate it via programmatically both options are tedious. At such time it becomes necessary to store the data in a manner that can be later retrieved and displayed either in part or in whole. This medium is usually a ''file'' on the disk.
Introduction to file
Until now we have been using the functions such as scanf,printf, getch, putch etc to read and write data on the variable and arrays for storing data inside the programs. But this approach poses the following programs.
1. The data is lost when program terminated or variable goes out of scope.
2. Difficulty to use large volume of data.
We can overcome these problems by storing data on secondary devices such as Hard Disk. The data is storage on the devices using the concept of ''file''.
A file is collection of related records, a record is composed of several fields and field is a group of character.
The most straightforward use of files is via a file pointer.
FILE *fp;
fp is a pointer to a file.
The type FILE, is not a basic type, instead it is defined in the header file stdio.h , this file must be included in your program.
2 File Operations
1. Create a new file.
2. Open an existing file
3. Read from file
4. Write to a file
5. Moving a specific location in a file(Seeking)
6. Closing File
10.1.3 Opening a File
fp = fopen(filename, mode);
The filename and mode are both strings.
Here modes can be
"r" read
"w" write, overwrite file if it exists
"a" write, but append instead of overwrite
"r+" read & write, do not destroy file if it exists
"w+" read & write, but overwrite file if it exists
"a+" read & write, but append instead of overwrite
"b" may be appended to any of the above to force the file to be opened in binary mode rather than text mode.
Eg.
FILE *fp;
fp=fopen(''input.txt'',''r'');
//Opens inputs.txt file in read mode
fclose(fp); //close file
Sequential file access is performed with the following library functions.
1. fopen() - Create a new file
2. fclose() - Close file
3. getc() - Read character from file
4. putc() - Write character to a file
5. getw() - Read Integer from file
6. putw() - Write Integer to a file
7. fprintf() - Write set of data values
8. fscanf() - Read set of data values
1.WRITING INTO A FILE :
#include<stdio.h> #include<conio.h> int main() { FILE *fp; fp=fopen("/temp/demo.txt","w"); fprintf(fp,"Hello friends, how are you???."); fclose(fp); return 0; }
Note=after compiling and executing thic programgo to C drive, open temp folder you will see a text
File(demo.txt)now open it ,the content of this file is Hello friends, how are you???
2.READING IN FILE:
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
char msg[100];
fp=fopen("/temp/demo.txt","r");//opening the file in reading mode
fgets(msg,100,fp);
printf("%s",msg);
fclose(fp);//closing the file
return 0;
}
•Output
Note=demo.txt file must be present in your temp folder of C Drive.
3.COPY CONTENT FROM ONE FILE TO OTHER:
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *sfile,*dfile;
char ch;
sfile=fopen("/temp/easy.txt","r");//opening the file in reading mode
dfile=fopen("/temp/soft.txt","w");//opening the file in writing mode
while((ch=getc(sfile))!=EOF)
putc(ch,dfile);//writing character by character
fclose(sfile);//closing the file
fclose(dfile);//closing the file
}
•Output
Note =easy.txt file must be present in your temp folder of C Drive so that its content can be copied into soft.txt file.
4.COUNT TOTAL NUMBER OF ALPHABETS IN A FILE :
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *sfile;
char ch;
int alpha=0;
sfile=fopen("/temp/easy.txt","r");//opening the file in reading mode
while((ch=getc(sfile))!=EOF)
{
if(ch>=65&&ch<=97||ch>=97&&ch<=122)
alpha++;
}
printf("Total number of alphabets=%d",alpha);
fclose(sfile);//closing the file
}
•Output
Note=easy.txt file must be present in your temp folder of C Drive
ASCII value of A=65,B=66...Z=90
ASCII value of A=97,B=98...Z=112
5.COUNT TOTAL NUMBER OF DIGITS IN A FILE :
#include<stdio.h>\n" +
"#include<conio.h>\n" +
"int main()\n" +
"{\n" +
"FILE *sfile;\n" +
"char ch;\n" +
"int digit=0;\n" +
"sfile=fopen(\"/temp/easy.txt\",\"r\");//opening the file in reading mode\n" +
"while((ch=getc(sfile))!=EOF)\n" +
"{\n" +
" if(ch>=48&&ch<=57)\n" +
" digit++;\n" +
"}\n" +
"printf(\"Total number of digits=%d\",digit);\n" +
"fclose(sfile);//closing the file\n" +
"}
•Output
Note=easy.txt file must be present in your temp folder of C Drive
ASCII value of 0=48,1=49....9=47.
6.COUNT TOTAL NUMBER OF SPECIAL SYMBOL IN A FILE:
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *sfile;
char ch;
int special=0;
sfile=fopen("/temp/easy.txt","r");//opening the file in reading mode
while((ch=getc(sfile))!=EOF)
{
if(ch>=65&&ch<=97||ch>=97&&ch<=122)
{}
else if(ch>=48&&ch<=57)
{}
else
special++;
}
printf("Total number of special symbol=%d",special);
fclose(sfile);//closing the file
}
•OUTPUT
Note=easy.txt file must be present in your temp folder of C Drive.
Comments