The fputs()
and fgets()
functions are commonly used in the C programming language for reading and writing strings to files. They are part of the standard I/O library and provide a convenient way to handle file input and output.
Here’s a brief explanation of each function:
fputs()
: Thefputs()
function is used to write a string to a file. Its syntax is as follows:
int fputs(const char *str, FILE *stream);
The str
parameter is the string to be written to the file, and stream
is a pointer to the FILE
object representing the file. It returns a non-negative value on success and EOF
(end-of-file) on failure.
Example usage:
FILE *file = fopen("file.txt", "w"); if (file != NULL) { if (fputs("Hello, World!", file) != EOF) { // Write successful } fclose(file); }
2. fgets()
: The fgets()
function is used to read a string from a file. Its syntax is as follows:
char *fgets(char *str, int size, FILE *stream);
The str
parameter is a character array where the read string will be stored, size
is the maximum number of characters to be read, and stream
is a pointer to the FILE
object representing the file. It returns str
on success and NULL
on failure or when the end of the file is reached.
Example usage:
FILE *file = fopen("file.txt", "r"); if (file != NULL) { char buffer[100]; if (fgets(buffer, sizeof(buffer), file) != NULL) { // Read successful } fclose(file); }
Note that both functions require the file to be opened using fopen()
in the appropriate mode ("w"
for writing or "r"
for reading) prior to calling them. Additionally, proper error handling should be implemented to check for potential failures in file opening or I/O operations.
Writing File : fputs() function
Certainly! Here’s an example of using the fputs()
function to write a string to a file in C:
#include <stdio.h> int main() { FILE *file = fopen("file.txt", "w"); if (file != NULL) { char str[] = "Hello, World!"; if (fputs(str, file) != EOF) { printf("String written to the file successfully.\n"); } else { printf("Error writing to the file.\n"); } fclose(file); } else { printf("Error opening the file.\n"); } return 0; }
In this example:
- We include the
<stdio.h>
header file, which contains the necessary definitions for file operations. - We open the file “file.txt” in write mode using
fopen()
. If the file is opened successfully, aFILE
pointer (file
) is returned. Otherwise, we display an error message. - We define a string
str
that we want to write to the file. - We call
fputs()
to write the string to the file. Iffputs()
returns a value other thanEOF
, it means the write operation was successful, and we print a success message. Otherwise, we display an error message. - Finally, we close the file using
fclose()
to free up system resources.
Make sure to include proper error handling and check the return values of file operations for robust code.
Reading File : fgets() function
Certainly! Here’s an example of using the fgets()
function to read a string from a file in C:
#include <stdio.h> int main() { FILE *file = fopen("file.txt", "r"); if (file != NULL) { char buffer[100]; if (fgets(buffer, sizeof(buffer), file) != NULL) { printf("String read from the file: %s\n", buffer); } else { printf("Error reading from the file.\n"); } fclose(file); } else { printf("Error opening the file.\n"); } return 0; }
In this example:
- We include the
<stdio.h>
header file, which contains the necessary definitions for file operations. - We open the file “file.txt” in read mode using
fopen()
. If the file is opened successfully, aFILE
pointer (file
) is returned. Otherwise, we display an error message. - We define a character array
buffer
with a size of 100. This array will store the string read from the file. - We call
fgets()
to read a string from the file into thebuffer
. Thesizeof(buffer)
argument specifies the maximum number of characters to read from the file. - If
fgets()
returns a non-NULL value, it means the read operation was successful. We print the string read from the file usingprintf()
. Otherwise, we display an error message. - Finally, we close the file using
fclose()
to free up system resources.
Make sure to include proper error handling and check the return values of file operations for robust code.