File Handling in C
File Handling in C
C Language
By Muhammad Ahmad (25)
BS SE
DEPARTMENT OF COMPUTING
101001101001000010101
0011110111011011011010
101000011100101011001
F
010100111010100010101
ile
Handlin
0001011010110110110100
010101110001010100010
1000101110101100010011
010011010010000101010
g
0111101110110110110101
010000111001010110010
101001110101000101010
0010110101101101101001
File handling is one of the most important parts of
programming. A file is a container in computer storage
devices used for storing data. File handling in C enables us to
create, update, read, and delete the files stored on the local
file system through our C program.
DATA
Why files are needed?
Why files
are
needed?
• When a program is terminated, the entire
data is lost. Storing in a file will preserve
your data even if the program terminates.
FILE *fptr;
Opening Modes in Standard I/O
The syntax for opening a file in standard I/O is:
ptr = fopen("fileopen","mode");
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.
• rb+ Open for both reading and writing in binary mode. If the file does not exist, fopen() returns NULL.
• 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 binary mode. If the file does not exist, it will be created.
Create a File
To create a file, you can use the w mode inside the fopen() function.
The w mode is used to write to a file. However, if the file does not exist, it will create one for you:
Example
FILE *fptr;
// Create a file
fptr = fopen("filename.txt", "w");
// Close the file
fclose(fptr);
Note: The file is created in the same directory as your other C files, if nothing else is specified.
This will close the file when we are done with it.
FILE *fptr;
It is considered as good practice, because it makes sure that: // Create a file
fptr = fopen("filename.txt", "w");
• Changes are saved properly // Close the file
• Other programs can use the file (if you want) fclose(fptr);
• Clean up unnecessary memory space
Write To a File
The w mode means that the file is opened for writing. To insert content to it, you can use the fprintf() function and add
the pointer variable (fptr in our example) and some text:
Example
FILE *fptr;
As a result, when we open the file on our computer, it looks like this:
Note: If you write to a file that already exists, the old content is deleted, and the new content is inserted. This is important to know, as you
might accidentally erase existing content.
For example:
fprintf(fptr, "Hello World!");
As a result, when we open the file on our computer, it says "Hello World!" instead of "Some text":
The a mode appends content at the end of the file: As a result, when we open the file on our computer, it looks like this:
Example
FILE *fptr;
Example
FILE *fptr;
Example
FILE *fptr;
In order to read the content of filename.txt, we can use the fgets() function.
The fgets() function takes three parameters:
Example
fgets(myString, 100, fptr);
1.The first parameter specifies where to store the file content, which will be in the myString array we just created.
2.The second parameter specifies the maximum size of data to read, which should match the size of myString (100).
3.The third parameter requires a file pointer that is used to read the file (fptr in our example).
Now, we can print the string, which will output the content of the file:
Example
FILE *fptr;
// Print the file content Note: The fgets function only reads the first line of the file. If
printf("%s", myString); you remember, there were two lines of text in filename.txt.
// Close the file
fclose(fptr);
Hello World!
To read every line of the file, you can use a while loop:
Example
FILE *fptr;
Example
If the file exist, read the content and print it. If the file does not exist, print a message:
FILE *fptr;