Print to File in C
The simplest way to print to a file in C is by using standard file I/O functions provided in the stdio.h
library, such as fprintf
, fputs
, and fwrite
.
In this tutorial, we will explore multiple scenarios for printing text to a file using different functions and approaches, making it easy for beginners to understand.
Example 1: Using fprintf
to Write Formatted Text to a File
In this example, we will open a file in write mode and use fprintf
to print formatted text into the file.
main.c
#include <stdio.h>
int main() {
FILE *fp = fopen("output.txt", "w"); // Open file in write mode
if (fp == NULL) {
perror("Error opening file");
return 1;
}
// Write formatted text to the file using fprintf
fprintf(fp, "Hello, World! This is a test: %d, %f\n", 100, 3.14);
fclose(fp); // Close the file
return 0;
}
Explanation:
- The variable
fp
is a pointer to aFILE
object, obtained by callingfopen
with the file name"output.txt"
and mode"w"
(write mode). fprintf
is used to write formatted text to the file. Here, it prints a string with an integer (100
) and a float (3.14
).fclose
is called to close the file once writing is complete, ensuring that all buffers are flushed and resources are freed.
Output:
Hello, World! This is a test: 100, 3.140000
Example 2: Using fputs
to Write a String to a File
In this example, we will open a file in write mode and use fputs
to write a simple string to the file.
main.c
#include <stdio.h>
int main() {
FILE *fp = fopen("output.txt", "w"); // Open file in write mode
if (fp == NULL) {
perror("Error opening file");
return 1;
}
// Write a string to the file using fputs
fputs("This is a simple string written using fputs.\n", fp);
fclose(fp); // Close the file
return 0;
}
Explanation:
- The file is opened in write mode using
fopen
, and a pointerfp
is assigned. fputs
is used to write a string directly to the file. Unlikefprintf
, it does not support formatting.- The file is then closed using
fclose
to ensure all data is properly saved.
Output:
This is a simple string written using fputs.
Example 3: Appending Text to a File
In this example, we will open a file in append mode so that we can add new content to the end of an existing file without overwriting the existing data.
main.c
#include <stdio.h>
int main() {
FILE *fp = fopen("output.txt", "a"); // Open file in append mode
if (fp == NULL) {
perror("Error opening file");
return 1;
}
// Append text to the file using fprintf
fprintf(fp, "Appending new content to the file.\n");
fclose(fp); // Close the file
return 0;
}
Explanation:
- The file is opened in append mode (
"a"
) usingfopen
. This mode allows data to be added to the end of the file. fprintf
is then used to write a line of text that will be added to any existing content in the file.- Finally, the file is closed using
fclose
to complete the operation.
Output:
Appending new content to the file.
Conclusion
In this tutorial, we learned multiple ways to print text to a file in C using various functions such as fprintf
and fputs
. We also explored how to open a file in different modes (write and append) to suit different needs.