Write Formatted Text to a File in C

To write formatted text to a file in C, you can use functions like fprintf() or combine sprintf() with fputs(). These functions allow you to format data similarly to printf() and write the resulting string directly into a file.


Example 1: Writing Formatted Text Using fprintf()

In this example, we will open a file named output.txt, write a formatted string that includes a name, age, and score using fprintf(), and then close the file.

main.c

</>
Copy
#include <stdio.h>

int main() {
    // Open file in write mode
    FILE *fp = fopen("output.txt", "w");
    if (fp == NULL) {
        perror("Error opening file");
        return 1;
    }
    
    // Variables for formatted output
    char name[] = "John";
    int age = 25;
    float score = 87.5;
    
    // Write formatted text to the file
    fprintf(fp, "Name: %s, Age: %d, Score: %.1f\n", name, age, score);
    
    // Close the file
    fclose(fp);
    
    return 0;
}

Explanation:

  1. FILE *fp = fopen("output.txt", "w"); opens the file output.txt in write mode. If the file does not exist, it is created.
  2. char name[] = "John";, int age = 25;, and float score = 87.5; declare and initialize variables to be written.
  3. fprintf(fp, "Name: %s, Age: %d, Score: %.1f\n", name, age, score); formats the string and writes it into the file. Here, %s is for a string, %d for an integer, and %.1f for a float with one decimal precision.
  4. fclose(fp); closes the file, ensuring that the data is properly saved.

Output:

Name: John, Age: 25, Score: 87.5

Example 2: Writing Formatted Text Using sprintf() and fputs()

In this example, we will use sprintf() to format a string and store it in a buffer, then open a file named output2.txt and write the formatted string using fputs().

main.c

</>
Copy
#include <stdio.h>

int main() {
    // Buffer to hold the formatted string
    char buffer[100];
    
    // Variables for formatting
    char name[] = "Alice";
    int year = 2025;
    
    // Format the string and store it in buffer
    sprintf(buffer, "User: %s, Year: %d", name, year);
    
    // Open file in write mode
    FILE *fp = fopen("output2.txt", "w");
    if (fp == NULL) {
        perror("Error opening file");
        return 1;
    }
    
    // Write the formatted string from buffer to the file
    fputs(buffer, fp);
    
    // Close the file
    fclose(fp);
    
    return 0;
}

Explanation:

  1. char buffer[100]; declares a character array to hold the formatted string.
  2. sprintf(buffer, "User: %s, Year: %d", name, year); formats the string using the variables and stores it in buffer.
  3. FILE *fp = fopen("output2.txt", "w"); opens the file output2.txt in write mode, creating it if it doesn’t exist.
  4. fputs(buffer, fp); writes the string stored in buffer to the file.
  5. fclose(fp); closes the file, ensuring the data is saved correctly.

Output:

User: Alice, Year: 2025

Conclusion

In this tutorial, we demonstrated two approaches to writing formatted text to a file in C: using fprintf() for direct formatted output and using a combination of sprintf() with fputs() for more flexible string handling.