Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. C Language Dictionary
  3. fgets() / fputs() / fgetc() / fputc()

fgets() / fputs() / fgetc() / fputc()

Functions for reading and writing text files one line or one character at a time. Commonly used for parsing log files and processing text data.

Syntax

// Reads one line from a file (including the newline character).
// Return value: buf on success, NULL on EOF or error.
char *fgets(char *buf, int size, FILE *stream);

// Writes a string to a file ('\0' is not written).
// Return value: a non-negative value on success, EOF on error.
int fputs(const char *str, FILE *stream);

// Reads one character from a file.
// Return value: the character read (cast to int as unsigned char), or EOF on error or end of file.
int fgetc(FILE *stream);

// Writes one character to a file.
// Return value: the character written, or EOF on error.
int fputc(int c, FILE *stream);

Function List

FunctionTargetDescription
fgets()Line readReads up to size - 1 characters and appends '\0' at the end. Prevents buffer overflows.
fputs()String writeWrites a string to a file. A newline is not appended automatically — add one manually if needed.
fgetc()Character readReads one character at a time. The return type is int so that EOF (-1) can be distinguished from valid unsigned char values.
fputc()Character writeWrites one character. Use putchar() as a shorthand when writing to standard output.
getchar()Standard inputShorthand for fgetc(stdin).
putchar()Standard outputShorthand for fputc(c, stdout).

Sample Code

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

int main(void) {
    // Write multiple lines to a file using fputs.
    FILE *fp = fopen("lines.txt", "w");
    if (fp == NULL) { perror("fopen"); return 1; }
    fputs("apple\n", fp);
    fputs("banana\n", fp);
    fputs("cherry\n", fp);
    fclose(fp);

    // Read the file line by line using fgets.
    fp = fopen("lines.txt", "r");
    if (fp == NULL) { perror("fopen"); return 1; }
    char buf[256];
    int line_num = 1;
    while (fgets(buf, sizeof(buf), fp) != NULL) {
        // fgets includes the newline character, so strip it from the end.
        buf[strcspn(buf, "\n")] = '\0';
        printf("%d: %s\n", line_num++, buf);
    }
    fclose(fp);
    // Output: 1: apple / 2: banana / 3: cherry

    // Count characters one at a time using fgetc.
    fp = fopen("lines.txt", "r");
    if (fp == NULL) { perror("fopen"); return 1; }
    int ch, char_count = 0;
    while ((ch = fgetc(fp)) != EOF) {
        char_count++;
    }
    fclose(fp);
    printf("Character count (including newlines): %d\n", char_count); // Prints the total number of characters in the file.

    return 0;
}

Notes

Never use gets(). Because it has no way to specify a buffer size, reading a long line will cause a buffer overflow. Always use fgets(), which lets you specify the buffer size.

Always receive the return value of fgetc() in an int variable. If you store it in a char, EOF (-1) may be confused with 255 on platforms where char is signed, causing an infinite loop.

For reading and writing binary files, use fread() / fwrite() since no newline conversion is needed.

If you find any errors or copyright issues, please .