Chap 9 File Handling
Chap 9 File Handling
Objectives
Understand streams
Master file system basics
Understand random access
Apply various file-system functions
Develop a simple file-based system
FILE *fp;
Each file we open will have its own file structure. The
file structure contains information about the file being
used, such as its current size, memory location, access
modes, etc.
FILE *fp;
Fp = fopen (“myfile.txt”, “r”);
Legal values for modes that can be used with fopen () are
summarised below.
“r” Searches file. If the file exists, loads it into memory and sets up a pointer
which points to the first character in it. If the file doesn’t exist it returns NULL.
“w” Searches file. If the file exists, its contents are overwritten. If the file
doesn’t exist, a new file is created; Returns NULL if unable to open file.
“a” Searches file. If the file exists, loads it into memory and sets up a pointer
which points to the first character in it. If the file doesn’t exist, a new file is
created. Returns NULL if unable to open file.
“r+” Searches file. If the file exists, loads it into memory and sets up a pointer
which points to the first character in it. Returns NULL if unable to open file.
Operations possible – Reading existing contents, writing new contents,
modifying contents of the file.
“w+” Searches file. If the file exists, its contents are destroyed. If the file
doesn’t exist, a new file is created. Returns NULL if unable to open file.
Operations possible – Writing new contents, reading them back and modifying
existing contents of the file.
“a+” Searches a file. If the file exists, loads it into memory and sets up a
pointer which points to the first character in it. Returns NULL if unable to open
file.
FILE *fp;
fp = fopen (“myfile.txt”, “r”);
if (fp = = NULL)
{
printf(“Error opening file \n”);
exit();
}
fclose (fp);
1. #include <stdio.h>
2. #include <stdlib.h>
3. #include <conio.h>
4. main( )
5. {
6. FILE *fp;
7. char another =’Y’;
8. char name[40];
9. int age;
10. float bs;
11.
1. {
2. printf(“Cannot open file”);
3. exit(1);
4. }
5.
6. while(another==’Y’)
7. {
8. printf(“\n Enter name, age and basic salary \n”);
9. scanf(“%s%d%f”, name,&age,&bs);
10. fprintf (fp, ”%s%d%f\n”, name,&age,&bs);
11. printf(“\n Another employee (Y/N)? ”);
12. fflush(stdin);
1. another=getche ();
2. }
3.
4. fclose (fp);
5. return 0;
6. }
1. #include <stdio.h>
2. #include <stdlib.h>
3. main()
4. {
5. FILE *fp;
6. char name[40];
7. int age;
8. float bs;
9.
1. {
2. printf(“cannot open file”);
3. exit(1);
4. }
5.
9. fclose (fp);
10. return 0;
11. }
Ongiri 34 1550.000000
Mutua 24 1200.000000
Halima 26 2000.000000
1. struct emp e;
2. fp = fopen (“EMPLOYEE.DATA”,”w”);
3.
4. if(fp==NULL)
5. {
6. printf(“cannot open file”);
7. exit(1);
8. }
9.
10. while(another==’Y’)
1. {
2. printf(“\n Enter name, age and basic salary: ”);
3. scanf(“%s%d%f“, e.name,&e.age,&e.bs);
4. fprintf (fp, “%s %d %f\n”,e.name,e.age,e.bs);
5. printf(“\n Add another record (Y/N)? ”);
6. fflush(stdin),
7. another=getche (); }
8. fclose (fp);
9. return 0;
10. } }
5. main()
6. {
7. FILE *fp;
8. struct emp
9. {
10. char name[40];
11. int age;
12. float bs;
13. };
1. struct emp e;
2. fp = fopen (“EMPLOYEE.DATA”,”w”);
3.
4. if(fp==NULL)
5. {
6. printf(“cannot open file”);
7. exit(1);
8. }
9.
Expected output:
Kanja 34 1250.000000
Wanja 21 1300.000000
Mwashe 34 1400.000000
6. main( )
7. {
8. FILE *fp;
9. char another = ’Y’;
10. struct emp
11. {
12. char name[40];
13. int age;
14. float bs;
15. };
1. struct emp e;
2.
3. fp=fopen (“EMP.DAT”,”wb”);
4. if(fp==NULL)
5. {
6. printf(“cannot open file”);
7. exit(1);
8. }
9.
The information obtained about the employee from the key board is placed in the structure
variable e. then, the following statement writes to the structure to the file:
fwrite (&e,sizeof(e),1,fp);
Here, the first argument is the address of the structure to be written to the disk.
The second argument is the size of the structure in bytes. Instead of counting the bytes
occupied by the structure ourselves, we let the program do it for us by using the sizeof()
operator which gives the size of variable in bytes.
This keeps the program unchanged in event of change in the elements of the structure. The
third argument is the number of such structures that we want to write at one time. In this case,
we want to write only one structure at a time. Had we had an array of structures, for example,
we might want have wanted to write the entire array at once.
The last argument is the pointer to the file we want to write to.
struct emp e;
{
printf(“cannot open file”);
exit(1);
}
fseek(fp,-sizeof(e), SEEK_CUR);
Jane Kuria Inoorero University 07/05/24
Explanation..
47
Here, -sizeof (e) moves the pointer back by sizeof(e) bytes from the
current position. SEEK_CUR is a macro defined in “stdio.h”
Similarly, the following fseek() would place the pointer beyond the
last record in the file.
in fact –sizeof (e) or 0 are just the offsets which tell the compiler by
how many bytes should the pointer be moved from a particular
position. The third argument could be either SEEK_END,
SEEK_CUR or SEEK_SET all these act as reference from which
the pointer should be offset. SEEK_END means move the pointer
from the end of the file, SEEK_CUR means move the file in
reference to its current position and SEEK_SET means move the
pointer with reference to the beginning of the file.
position=ftell(fp);
Revision Exercise
1. Define an input file handle called input_file,
which is a pointer to a type FILE.
2. Using input_file, open the file results.dat for
read mode as a text file.
3. Write C statements which tests to see if
input_file has opened the data file successfully. If
not, print an error message and exit the program.