Lab Manual 12
Lab Manual 12
Lab Manual 12
Types of Files
When dealing with files, there are two types of files you should know about:
1. Text files
2. Binary files
1. Text files
Text files are the normal .txt files. You can easily create text files using any simple text editors
such as Notepad.
When you open those files, you'll see all the contents within the file as plain text. You can easily
edit or delete the contents.
They take minimum effort to maintain, are easily readable, and provide the least security and
takes bigger storage space.
2. Binary files
Instead of storing data in plain text, they store it in the binary form (0's and 1's).
They can hold a higher amount of data, are not readable easily, and provides better security
than text files.
File Operations
In C, you can perform four major operations on files, either text or binary:
1. FILE *fptr;
1. ptr = fopen("fileopen","mode");
For example,
● fopen("E:\\cprogram\\newprogram.txt","w");
●
● fopen("E:\\cprogram\\oldprogram.bin","rb");
● Let's suppose the file newprogram.txt doesn't exist in the location E:\cprogram. The first
function creates a new file named newprogram.txt and opens it for writing as per the
mode 'w'.
The writing mode allows you to create and edit (overwrite) the contents of
the file.
● Now let's suppose the second binary file oldprogram.bin exists in the location
E:\cprogram. The second function opens an existing file for reading in binary mode 'rb'.
The reading mode only allows you to read the file, you cannot write into
the file.
Mode Meaning of Mode During Inexistence of file
r Open for reading. If the file does not exist, fopen() returns
NULL.
rb Open for reading in binary mode. If the file does not exist, fopen() returns
NULL.
wb Open for writing in binary mode. If the file exists, its contents are
overwritten.
If the file
does not exist, it will be created.
ab Open for append in binary mode. If the file does not exist, it will be
Data is created.
added to the end of the file.
r+ Open for both reading and writing. If the file does not exist, fopen() returns
NULL.
rb+ Open for both reading and writing in If the file does not exist, fopen() returns
binary mode. NULL.
w+ Open for both reading and writing. If the file exists, its contents are
overwritten.
If the file
does not exist, it will be created.
wb+ Open for both reading and writing in If the file exists, its contents are
binary mode. overwritten.
If the file
does not exist, it will be created.
a+ Open for both reading and appending. If the file does not exist, it will be
created.
ab+ Open for both reading and appending in If the file does not exist, it will be
binary mode. created.
Closing a File
The file (both text and binary) should be closed after reading/writing.
1. fclose(fptr);
They are just the file versions of printf() and scanf(). The only difference is that fprint() and
fscanf() expects a pointer to the structure FILE.
This program takes a number from the user and stores in the file program.txt.
After you compile and run this program, you can see a text file program.txt created in C drive of
your computer. When you open the file, you can see the integer you entered.
1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. int main()
5. {
6. int num;
7. FILE *fptr;
8.
9. if ((fptr = fopen("C:\\program.txt","r")) == NULL){
10. printf("Error! opening file");
11.
12. // Program exits if the file pointer returns NULL.
13. exit(1);
14. }
15.
16. fscanf(fptr,"%d", &num);
17.
18. printf("Value of n=%d", num);
19. fclose(fptr);
20.
21. return 0;
22. }
This program reads the integer present in the program.txt file and prints it onto the screen.
If you successfully created the file from Example 1, running this program will get you the integer
you entered.
Other functions like fgetchar(), fputc() etc. can be used in a similar way.
To write into a binary file, you need to use the fwrite() function. The functions take four
arguments:
1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. struct threeNum
5. {
6. int n1, n2, n3;
7. };
8.
9. int main()
10. {
11. int n;
12. struct threeNum num;
13. FILE *fptr;
14.
15. if ((fptr = fopen("C:\\program.bin","wb")) == NULL){
16. printf("Error! opening file");
17.
18. // Program exits if the file pointer returns NULL.
19. exit(1);
20. }
21.
22. for(n = 1; n < 5; ++n)
23. {
24. num.n1 = n;
25. num.n2 = 5*n;
26. num.n3 = 5*n + 1;
27. fwrite(&num, sizeof(struct threeNum), 1, fptr);
28. }
29. fclose(fptr);
30.
31. return 0;
32. }
We declare a structure threeNum with three numbers - n1, n2 and n3, and define it in the main
function as num.
Now, inside the for loop, we store the value into the file using fwrite().
The first parameter takes the address of num and the second parameter takes the size of the
structure threeNum.
Since we're only inserting one instance of num, the third parameter is 1. And, the last parameter
*fptr points to the file we're storing the data.
Function fread() also take 4 arguments similar to the fwrite() function as above.
1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. struct threeNum
5. {
6. int n1, n2, n3;
7. };
8.
9. int main()
10. {
11. int n;
12. struct threeNum num;
13. FILE *fptr;
14.
15. if ((fptr = fopen("C:\\program.bin","rb")) == NULL){
16. printf("Error! opening file");
17.
18. // Program exits if the file pointer returns NULL.
19. exit(1);
20. }
21.
22. for(n = 1; n < 5; ++n)
23. {
24. fread(&num, sizeof(struct threeNum), 1, fptr);
25. printf("n1: %d\tn2: %d\tn3: %d", num.n1, num.n2,
num.n3);
26. }
27. fclose(fptr);
28.
29. return 0;
30. }
In this program, you read the same file program.bin and loop through the records one by one.
In simple terms, you read one threeNum record of threeNum size from the file pointed by *fptr
into the structure num.
This will waste a lot of memory and operation time. An easier way to get to the required data
can be achieved using fseek().
As the name suggests, fseek() seeks the cursor to the given record in the file.
Syntax of fseek()
The first parameter stream is the pointer to the file. The second parameter is the position of the
record to be found, and the third parameter specifies the location where the offset starts.
Whence Meaning
SEEK_C Starts the offset from the current location of the cursor in the file.
UR
Example 5: fseek()
1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. struct threeNum
5. {
6. int n1, n2, n3;
7. };
8.
9. int main()
10. {
11. int n;
12. struct threeNum num;
13. FILE *fptr;
14.
15. if ((fptr = fopen("C:\\program.bin","rb")) == NULL){
16. printf("Error! opening file");
17.
18. // Program exits if the file pointer returns NULL.
19. exit(1);
20. }
21.
22. // Moves the cursor to the end of the file
23. fseek(fptr, -sizeof(struct threeNum), SEEK_END);
24.
25. for(n = 1; n < 5; ++n)
26. {
27. fread(&num, sizeof(struct threeNum), 1, fptr);
28. printf("n1: %d\tn2: %d\tn3: %d\n", num.n1, num.n2,
num.n3);
29. fseek(fptr, -2*sizeof(struct threeNum), SEEK_CUR);
30. }
31. fclose(fptr);
32.
33. return 0;
34. }
This program will start reading the records from the file program.bin in the reverse order (last to
first) and prints it.
Exercises:
1. Write a C program to read name and marks of n number of students and store them in a
file.
4. Write a C program to write all the members of an array of structures to a file using
fwrite(). Read the array from the file and display on the screen.
Test Data :
Input the file name to be opened : test.txt
Input the line you want to remove : 2
Expected Output :
The content of the file test.txt is :
test line 1
test line 3
test line 4
Expected Output :
File test.txt successfully encrypted ..!!
If you read the file test.txt you will see the following :
������Ʉ�ӄۗ �������ɒ��ђn