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
| Function | Target | Description |
|---|---|---|
| fgets() | Line read | Reads up to size - 1 characters and appends '\0' at the end. Prevents buffer overflows. |
| fputs() | String write | Writes a string to a file. A newline is not appended automatically — add one manually if needed. |
| fgetc() | Character read | Reads one character at a time. The return type is int so that EOF (-1) can be distinguished from valid unsigned char values. |
| fputc() | Character write | Writes one character. Use putchar() as a shorthand when writing to standard output. |
| getchar() | Standard input | Shorthand for fgetc(stdin). |
| putchar() | Standard output | Shorthand 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 contact us.