C Program to Write a Sentence to a File

Writing to files is a common task in C programming, allowing data to be stored and retrieved efficiently. This article covers three different C Program to Write a Sentence to a File. Each method will be explained in detail, accompanied by example programs and outputs.

Prerequisites

Before diving into the examples, ensure you have the following prerequisites:

  • Basic understanding of C programming.
  • Familiarity with file handling functions in C.
  • A C compiler installed on your system (e.g., GCC).

Solution 1: Writing to a File Using fprint function

1.1 Explanation

The fprintf function is used to write formatted data to a file. This method allows for flexible formatting similar to the printf function.

1.2 Program

C
#include <stdio.h>

int main() {
    FILE *fptr;
    char sentence[100];

    // Open file in write mode
    fptr = fopen("output1.txt", "w");

    if (fptr == NULL) {
        printf("Error opening file!\n");
        return 1;
    }

    printf("Enter a sentence: ");
    fgets(sentence, sizeof(sentence), stdin);

    // Write the sentence to the file
    fprintf(fptr, "%s", sentence);

    // Close the file
    fclose(fptr);

    printf("Sentence written to file successfully.\n");
    return 0;
}

1.3 Output

C
Enter a sentence: Hello, world!
Sentence written to file successfully.

The content of output1.txt will be:

C
Hello, world!

Solution 2: Writing to a File Using fputs

2.1 Explanation

The fputs function is used to write a string to a file. This method is straightforward for writing a single string without any formatting.

2.2 Program

C
#include <stdio.h>

int main() {
    FILE *fptr;
    char sentence[100];

    // Open file in write mode
    fptr = fopen("output2.txt", "w");

    if (fptr == NULL) {
        printf("Error opening file!\n");
        return 1;
    }

    printf("Enter a sentence: ");
    fgets(sentence, sizeof(sentence), stdin);

    // Write the sentence to the file
    fputs(sentence, fptr);

    // Close the file
    fclose(fptr);

    printf("Sentence written to file successfully.\n");
    return 0;
}

Output

C
Enter a sentence: This is a test.
Sentence written to file successfully.

The content of output2.txt will be:

C
This is a test.

Solution 3: Writing to a File Using fwrite

3.1 Explanation

The fwrite function is used to write binary data to a file. While typically used for binary data, it can also be used for writing strings.

3.2 Program

C
#include <stdio.h>
#include <string.h>

int main() {
    FILE *fptr;
    char sentence[100];

    // Open file in write mode
    fptr = fopen("output3.txt", "wb");

    if (fptr == NULL) {
        printf("Error opening file!\n");
        return 1;
    }

    printf("Enter a sentence: ");
    fgets(sentence, sizeof(sentence), stdin);

    // Write the sentence to the file
    fwrite(sentence, sizeof(char), strlen(sentence), fptr);

    // Close the file
    fclose(fptr);

    printf("Sentence written to file successfully.\n");
    return 0;
}

Output

C
Enter a sentence: C programming is fun!
Sentence written to file successfully.

The content of output3.txt will be:

C
C programming is fun!

Conclusion

Writing to files is a fundamental skill in C programming. This article presented three methods to write a sentence to a file using fprintf, fputs, and fwrite. Each method has its advantages and use cases:

  • fprintf allows for formatted writing.
  • fputs is straightforward for writing simple strings.
  • fwrite is powerful for binary data but can also handle strings.

Understanding these methods enhances your ability to manage file I/O operations efficiently in C.