C fputs() Function | Write a String to a File

Introduction

The fputs() function is a straightforward and efficient way to write strings to a stream. It does not append a newline character automatically, making it suitable for writing precise string data without any additional characters.

fputs() Function Syntax

The syntax for the fputs() function is as follows:

int fputs(const char *str, FILE *stream);

Parameters:

  • str: A pointer to a null-terminated string to be written to the stream.
  • stream: A pointer to a FILE object that specifies an output stream.

Returns:

  • The function returns a non-negative value on success. If an error occurs, EOF is returned.

Understanding fputs()

The fputs() function writes the string str to the specified stream. It does not include the null terminator in the output. This function is useful when you need to write strings to files or other output streams without appending a newline character.

Examples

Writing a String to a File

To demonstrate how to use fputs() to write a string to a file, we will write a simple program.

Example

#include <stdio.h>

int main() {
    FILE *file;

    // Open the file for writing
    file = fopen("example.txt", "w");
    if (file == NULL) {
        printf("Error: Could not open file for writing.\n");
        return 1;
    }

    // Write a string to the file
    if (fputs("Hello, World!", file) == EOF) {
        printf("Error: Could not write to file.\n");
        fclose(file);
        return 1;
    }

    // Close the file
    fclose(file);

    return 0;
}

Output (content of example.txt):

Hello, World!

Writing a String to Standard Output

This example shows how to use fputs() to write a string to the standard output.

Example

#include <stdio.h>

int main() {
    // Write a string to the standard output
    if (fputs("Hello, World!\n", stdout) == EOF) {
        printf("Error: Could not write to standard output.\n");
        return 1;
    }

    return 0;
}

Output:

Hello, World!

Real-World Use Case

Writing Logs to a File

In real-world applications, the fputs() function can be used to write log messages to a log file.

Example

#include <stdio.h>
#include <time.h>

void log_message(const char *message) {
    FILE *logfile = fopen("log.txt", "a");
    if (logfile == NULL) {
        printf("Error: Could not open log file.\n");
        return;
    }

    time_t now = time(NULL);
    fprintf(logfile, "%s: ", ctime(&now));
    if (fputs(message, logfile) == EOF) {
        printf("Error: Could not write to log file.\n");
    }

    // Close the log file
    fclose(logfile);
}

int main() {
    log_message("Application started.\n");
    log_message("An event occurred.\n");

    return 0;
}

Output (content of log.txt):

Wed Jul  4 12:34:56 2023: Application started.
Wed Jul  4 12:34:57 2023: An event occurred.

Conclusion

The fputs() function in C is a standard library function that writes a string to the specified stream. It is part of the C standard library (stdio.h) and is commonly used for writing strings to files or the standard output (stdout).

Comments