MCS-201 · Unit 8

File Handling

8 sections

Block 2 · Unit 8File Handling89 min · 8 sections

Unit 8File Handling

Follows IGNOU MCS-201, Unit 8

Variables live in main memory, so their values disappear when the program ends. To keep data between runs, a program must store it in a file on disk. This unit shows how C programs open files, read and write them in several ways (character by character, line by line, formatted, and as blocks of records), jump to any position in a file, and use the lower-level system routines that sit underneath the standard library.

In this unit you will learn to

  • Explain streams, text files, binary files and the FILE pointer.
  • Open and close files with fopen and fclose, choosing the right mode and checking for errors.
  • Read and write characters, lines and formatted data with getc/putc, fgets/fputs and fscanf/fprintf.
  • Store and read whole records in binary files with fwrite and fread.
  • Detect the end of a file correctly, without the common feof mistake.
  • Move around a file with fseek, ftell and rewind to read and update records directly.
  • Use the low-level routines open, read, write, lseek and close.

Sections in this unit

  1. 8.1Files, Streams and File PointersWhy programs need files, what a stream is, how text and binary files differ, and how a FILE pointer connects a program to a file.10 min
  2. 8.2Opening and Closing FilesOpen files with fopen in the right mode, check for failure, and close them with fclose so buffered data is saved.10 min
  3. 8.3Character Input and OutputRead and write one character at a time with getc, putc, fgetc and fputc, understand EOF, and use feof correctly.13 min
  4. 8.4String Input and OutputRead and write whole lines with fgets and fputs, use their return values, and count the lines in a file correctly.10 min
  5. 8.5Formatted Input and OutputWrite and read numbers and strings in text files with fprintf and fscanf, and use their return values to control loops.11 min
  6. 8.6Block Input and OutputWrite and read whole records or arrays in binary files with fwrite and fread, and use their return values to read until the end.8 min
  7. 8.7Random Access and Positioning the File PointerCompare sequential and random access, move within a file with fseek, ftell and rewind, and update a record in place.15 min
  8. 8.8Unbuffered (Low-Level) File I/OUse the system-level routines open, creat, read, write, lseek and close with file descriptors, and compare them with the buffered functions.12 min
↑ ↓ move↵ openTab switch type