The C programming language contains several features and functions that aid in more efficient and effective development. Usually, we merely compiled and ran the program, but we didn't save the output anywhere.
However, suppose you want to save the output for later use. So, what are your options?
This is where C's file handling functionality comes in handy. You can save the output or data to a volatile local file system that can be accessed anytime. Functions like fputs() and fgets() comes in handy when dealing with the file.
What is File Handling?
A file is a container in computer storage devices that is used to permanently store output or information as a sequence of bytes on the disks.
You can create, delete, open, read, and manipulate data within files in C. You can also use data from a file to deal with it in your software code.
This article will cover two essential functions of C, fputs() and fgets() for writing and reading.
In C programming, the functions fputs() and fgets() are used to write and read strings from a stream. Let's look at some instances of utilizing the fputs() and fgets() functions to write and read files.
If you want to inspect the output of a program numerous times, compiling and running the program each time would be a tedious operation, that’s when functions like fputs() and fgets() come in handy. You may easily address this problem with the aid of file handling.
Reusability: Data is lost when a program is terminated, hence reusability is important. You can save your data to a file so that you don't lose it if the program crashes.
Portability: You can effortlessly transfer the file's data from one machine to another without making any changes.
Time-Saving:- Assume you have a significant amount of data to enter. However, entering them all will take a long time. This problem can be simply solved by saving all of the data in a file and then accessing it with a few C commands.
Large storage capacity: You may store a lot of data in a file with the help of files. You don't have to be concerned about storage space.
Writing File Using fputs() Function
You can write a line of characters into a file using the fputs() method.
The function starts copying from the supplied address (str) and continues until it hits the null character ('\0'). This null character at the end of the stream is not copied to the stream.
Not only do fputs differ from puts in that the target stream can be specified, but it also does not write any further characters, whereas puts automatically append a newline character at the end.
Syntax
int fputs ( const char * str, FILE * stream );
Parameters
str: The content to be written to the stream is a C string.
FILE: An output stream is identified by a pointer to a FILE object.
Return Value
A non-negative value is returned if the operation succeeds.
The function returns EOF and sets the error indicator when it encounters an error (ferror).
Example
#include<stdio.h>
#include<conio.h>
void main(){
FILE *file;
file = fopen(“output.txt","w");
fputs("This is a simple string!",file);
fclose(file);
getch();
}
Output:
The line "This is a simple string!" will be written in a file called output.txt after the preceding software has been compiled and run.
You can read a line of characters from a file using the fgets() method.
Reads characters from stream and stores them in str as a C string until (num-1) characters have been read or a newline or the end of the file has been reached, whichever comes first.
A newline character causes fgets() to cease reading, but the function treats it as a legitimate character and includes it in the text copied to str.
After the characters are copied to str, a concluding null character is automatically inserted.
The fputs() method transfers the current place of string to the output stream. The null character (0) at the end of the string is not copied.
Is there any file handling in C?
File handling is the process of storing data in a file with the help of a program. The programs in the C programming language use file handling to save the program's results and other data to a file.
What are the different file types in C?
The C programming language supports two different sorts of files:
ASCII Files (or Text Files).
Binary files
In C, what is a file pointer?
A file pointer is a type of pointer that is used to manage and track the files that are being accessed. A new data type named "FILE" is used to declare a file pointer. The stdio.h file defines this data type.
What purpose does fputs() and fgets() serve?
In C programming, the functions fputs() and fgets() are used to write and read strings from a stream. There are more functions in C for specific tasks; fputs() and fgets() are the most common.
Conclusion
This article covers everything you need to know about fputs() and fgets() in C. Want to learn more? Here are more articles for rescue.