Working with files is a must-have skill in C. This time, we’ll write a sentence to a file. Imagine needing to save user input directly to disk for later use. Let’s check out how to do that with just a few lines of code.
Prerequisites for the C Program to Write a Sentence to a File
Before diving into the code, ensure you know the following:
- File handling functions in C like
fopen,fprintf, andfclose. - Basic knowledge of
stdio.handstdlib.hheaders. - Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler.
Source Code of C Program to Write a Sentence to a File
Here's the complete code to write a sentence to a file. It opens a file, writes input data, and then closes the file.
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *file = fopen("output.txt", "w");
// Open file in write mode
if (file == NULL) {
printf("Error opening file!\n");
exit(1);
}
char sentence[100];
printf("Enter the sentence to write to the file: ");
fgets(sentence, 100, stdin);
fprintf(file, "%s", sentence);
printf("Sentence written to file successfully.\n");
fclose(file);
return 0;
}Try this code yourself — free
Run it in the EduSeekho online compiler. No setup needed.
Output of the Write a Sentence to a File Program
When you run the program, it prompts for a sentence and writes it to a file. Here's what it looks like:
Enter the sentence to write to the file: Hello, EduSeekho!
Sentence written to file successfully.
Algorithm to Write a Sentence to a File in C
This program writes a sentence to a file using basic file handling operations in C. Let's break down the steps involved.
- Open the file
output.txtin write mode. - Check if the file is opened successfully. If not, print an error message and exit.
- Prompt the user to enter a sentence.
- Write the entered sentence to the file.
- Close the file.
flowchart TD A([Start]) --> B[Open file in write mode] B --> C[Check if file is NULL] C -->|No| D[Prompt user for input] D --> E[Write sentence to file] E --> F[Close the file] F --> G([End]) C -->|Yes| H[Print error message] H --> G
How the C Program to Write a Sentence to a File Works
Let's examine each part of the code to understand how it works.
#include <stdio.h>and#include <stdlib.h>: These headers are necessary for file operations and standard functions.FILE *file = fopen("output.txt", "w");: Opens the file in write mode. If the file doesn't exist, it creates one.if (file == NULL): Checks if the file was opened successfully. If not, it prints an error message.fgets(sentence, 100, stdin);: Reads a sentence from the user input.fprintf(file, "%s", sentence);: Writes the sentence to the file.fclose(file);: Closes the file to save resources.
Quick Quiz — test your understanding
Tap an option to check your answerWhat function is used to open a file in C?
Time and Space Complexity of the Write a Sentence to a File Program
Let's look at how this program performs in terms of time and space complexity.
Time Complexity: O(n). Here, n is the number of characters in the input sentence. The operation involves reading character by character until a newline is found or the limit is reached. For example, writing a 50-character sentence takes 50 operations.
Space Complexity: O(1). The program uses fixed memory for variables, like the sentence array to store input, and the space does not grow with input size.
Common mistake: Forgetting to close the file with fclose(file); can lead to memory leaks and file corruption.
Key takeaway: This program is a simple yet powerful demonstration of file operations in C. Always make sure to close the file properly to dodge issues.
FAQs About C Program to Write a Sentence to a File
Here are some common questions students ask about this program.
stdio.h for file stuff and stdlib.h for standard functions.sentence array. You can make this size bigger if you want.fopen function call.Related Programs
If you found this program helpful, check out these related programs:
- C Program to Read the First Line From a File
- C Program to Write Hello World - Full Code and Output
- C Program to Find the Size of int, float, double and char
- C Program to Display its own Source Code as Output
- C Program to Store Information of Students Using Structure
Conclusion
Writing to files is super important for many apps. This program lays the groundwork for more advanced file operations. Try changing the filename or sentence limit to see how it works.
Practice your skills with more C programs in our C language quiz or check out other examples in the C online compiler.