Guided Notes

File Handling

Opening files with fopen and modes, writing with fprintf and fputs, reading with fgets and fscanf, closing, and checking for errors.

Opening and closing files

Slide 1 / 5

Work with files through a FILE pointer:

FILE *f = fopen("notes.txt", "w"); if (f == NULL) { perror("notes.txt"); return 1; } fprintf(f, "Hello, file!\n"); fclose(f);

Always check fopen's result — files can be missing, locked, or not permitted. Always fclose: it flushes buffered data to disk and releases the file.

← Dynamic Memory Management