File HandlingIn computer programming, a file is a storage device. It is essential in the following ways:
- You may lose your data when a program is terminated. Storing your data in a file can save you from this stress
- Saving data in a file makes them easily accessible. In C, the programmer can use a few commands to access the contents of a file.
- Lastly, a file allows you to move your data from one computer to another.
- Text files
- Binary Files
- File creation
- Opening of an existing file
- Closing a file
- Writing information to and reading from a file
- Creating or opening a file
The fopen function is used to create or open a file with the required access modes. The following are some of the commonly used file access modes in C:
“r” – Searches if a file and tries to open it successfully. Then, fopen() loads it into the memory and sets up a pointer. The pointer usually points to the first character on the file. The fopen() returns NULL if the file cannot be opened.
“w” – This mode searches a file and overwrites its contents if it exists. NULL is returned if the file cannot be opened. However, if it doesn’t exist, a new file will be created.
“a” – searches a file and have the fopen() load it into the memory if it opens successfully. The function also sets up a pointer that points to the last character in the file. It creates a new file if the searched one does not exist and returns NULL if it cannot open the file.
The modes mentioned above are for text files. If you want to perform some operations on a binary file, then you need to add b at the end of each mode. For example, in “a,” you have to use “ab,” in “r” use “rb” and in “w” use “wb.”
A special pointer known as the File pointer is used to perform operations in a file. We can declare it as shown below:
We can open a file in the following manner:
Filepointer = fopen (“file name.txt”, “w”);
Reading from a file
The fscanf and the fgets functions areused to perform the file read operations. Although both of these functions perform the same operations as the scanf function, they have an additional parameter, which is the file pointer. It is upon you to decide if you want to read the file character by character or line by line.
Below is the code snippet used to read a file:
filepointer = fopen (“filename.txt”, “r”);
fscanf (filePointer, “%s %s %s %d”, str1, str2, str3, &year);
Writing a file
The fprintf and the fputs functions are used to perform the file write operations. Our help with c programming assignment experts have coded the snippet for writing to a file below:
filepointer = fopen(“filename.txt”, “w”);
fprintf (filePointer, “%s %s %s %d”, “we”, “are”, “in”, 2020);
Closing a file
After performing successful file operations such as opening, reading and writing, you must always close a file. You have to use the fclose function for this operation. Here is a snippet for closing a file:
filepointer = fopen (“filename.txt”, “r”)
To summarize file handling, here is a program that opens a file, writes in it and closes it.
//A c program that opens a file, write in it and closes it
Int main ( )
//The first step is to declare the file pointer
//Next, get the data written in the file
= “Programmingassignmenthelper – The best assignment help website”;
//Use the fopen() to open the existing file codeTest.c
// use the w attribute in the write mode
filePointer = fopen (“codeTest.c”, “w”);
//confirm if the filePointer is NULL
//This may happen if the file does not exist
If (filePointer == NULL)
Printf (“codeTest.cfile does not exist”);
Printf (“The file has opened successfully.\n”);
// come up with the datatobewritten in the file
if (strlen ( datatobewritten ) > 0)
//use the fputs() to write in the file
fputs (datatobewritten, filePointer);
fputs ( “\n, filePointer);
//use the fclose() to close the file
printf ( “Your data was successfully written in the file codeTest.c \n”);
Printf (“You have now closed the file.”);
Do you need more code examples on this? Place your order with Programmingassignmenthelper.com or check out our free samples.