FPUTS()

this function is used to write a string to a file stream.

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

 * str: The string to write (must be null-terminated)
 * stream: Pointer to a FILE object (e.g., opened with fopen())
 
 * Writes the string as-is, without automatically appending a newline (\n)
 * Stops writing when it hits the null terminator (\0)
 * Does not add a newline — devs must include it explicitly if desired
 * Returns a non-negative value on success
 * Returns EOF (usually -1) on failure (e.g., if the stream is closed or unwritable)
 
 * COMPARISON
   fputs()	❌ No	❌ No	Simple string output
   fprintf()	❌ No	  Yes	Supports formatting (%d, etc.)
   puts()	  Yes	❌ No	Writes to stdout only
#include <stdio.h>

int main() {
    FILE *fp = fopen("log.txt", "w");  // open for writing
    if (fp == NULL) {
        perror("Failed to open file");
        return 1;
    }

    fputs("This is a log entry.\n", fp);  // write string with newline
    fputs("Another line without newline", fp);  // write string without newline

    fclose(fp);
    return 0;
}

Last updated