ProgrammingC Developer

Describe the process of working with file streams in C. How to open, read, write, and close files using the standard library? What issues exist when working with files and how to solve them?

Pass interviews with Hintsage AI assistant

Answer.

Background:

Working with files in C is a fundamental skill that originated in the early implementations of the language. The C standard library (stdio.h) provides universal functions for input-output stream operations, making programs portable across operating systems.

Issue:

Beginners often make mistakes when dealing with file streams: they do not check the result of file opening, improperly manage memory for buffering, and fail to handle read or write errors. Errors in closing files can lead to resource leaks, while incorrect handling of buffers can result in data loss.

Solution:

To work correctly with files, one should always:

  • open the file using fopen;
  • check for successful opening;
  • use fread, fwrite, fscanf, fprintf functions for reading/writing;
  • close the file with fclose.

Example code:

#include <stdio.h> int main() { FILE *fp = fopen("example.txt", "w"); if (!fp) { perror("Failed to open file"); return 1; } fprintf(fp, "Hello, world!\n"); fclose(fp); fp = fopen("example.txt", "r"); if (!fp) { perror("Error opening file for reading"); return 1; } char buffer[100]; while (fgets(buffer, sizeof(buffer), fp)) { printf("%s", buffer); } fclose(fp); return 0; }

Key features:

  • Working with file streams requires explicit opening and closing of files.
  • It is essential to always check the result of file operations.
  • File buffering affects performance and can lead to errors if not closed in time.

Tricky questions.

Can a file be closed multiple times using fclose?

No, double closing of a file stream leads to undefined behavior. After fclose, the descriptor becomes invalid.

What does the fread function return if it reaches the end of the file?

fread returns the number of successfully read elements. If the end of the file is reached — the returned count may be less than expected. Always check feof and ferror for diagnostics.

Can the same file stream be used for reading and writing simultaneously?

Yes, if the file is opened in "r+" or "w+" mode, however, it is necessary to call fflush or perform another file position movement operation (fseek) before changing direction (write/read). Otherwise, the behavior is undefined.

Common mistakes and anti-patterns

  • Not checking the success of file openings.
  • Not closing files, leading to resource leaks.
  • Using the same FILE * pointer after fclose.
  • Leaving the output buffer unsynchronized (fflush not called, stream not closed).

Real-life example

Negative case

A developer wrote data to a log file without checking the return value from fopen. After exhausting the descriptors, the file could not be opened anymore — log messages were lost.

Pros:

  • The code looked simple.

Cons:

  • Data was irretrievably lost when file descriptors ran out.
  • Errors went undiagnosed as return codes were not checked.

Positive case

In another version of the code, all return values (fopen, fwrite, fclose) were always checked. In case of error, a detailed diagnostic message was printed using perror, and the program exited correctly, releasing all resources.

Pros:

  • Reliable error handling.
  • Ease of debugging and guarantee of resource release.

Cons:

  • The code was slightly longer and required careful control of all errors.