πŸ“‚

Understanding Text Files in C Programming

Dec 4, 2024

Notes on Text Files in C

Learning Outcomes

  • Understand how to stream data using standard library functions to access persistent text.

Introduction

  • Secondary Storage: Retains information when a computer is turned off and allows access by different programs. Information is stored in the form of files.
  • Chapter Goals: Describe how to connect a program to a file, store and retrieve information.

Files

  • Definition: A named area of secondary storage.
    • Can be fragmented across non-contiguous locations.
    • Distinguished by an end-of-file mark (EOF, typically -1).

Text Format

  • Formats: Text and binary.
    • Text Format: Readable and editable data suitable for text editors. Portable across platforms with the same character set.
    • Character Set Standard: IEC/ISO 646-1083.
      • Includes letters, digits, space, control characters, and graphic characters (excludes $ and `).`

Sequential Access

  • Access data sequentially, byte by byte, until EOF is reached.

File Connections in C

  • Connecting to a File: Use a FILE type object to hold file information and track access position.
    • Declaration: #include <stdio.h> FILE *fp = NULL;
    • Pointer: Initialize to NULL to avoid premature dereferencing.*

Opening a File

  • Function: fopen() opens a file and returns a FILE object address.
    • Parameters: file_name[], mode[] (e.g., "r", "w", "a").
    • Example: Open "alpha.txt" for writing with fopen("alpha.txt", "w");
  • Failure: Returns NULL if it fails due to permissions or other issues.

Closing a File

  • Function: fclose() disconnects the file from the program.
    • Successful Return: 0; Unsuccessful: EOF.
    • Example: Close "alpha.txt" with fclose(fp);

File Communication in C

  • Functions:
    • fprintf(): Formatted write.
    • fputc(): Write single character.
    • fscanf(): Formatted read.
    • fgetc(): Read single character.

Writing

  • Formatted Writing: fprintf() controls format.
    • Prototype: int fprintf(FILE *, const char [], ...);
  • Unformatted Writing: fputc() writes a single character.
    • Prototype: int fputc(int ch, FILE *fp);

Reading

  • Formatted Reading: fscanf() reads bytes under format control.
    • Prototype: int fscanf(FILE *, const char [], ...);
  • Unformatted Reading: fgetc() reads a single character.*

Managing File State

  • Functions:
    • rewind(): Resets pointer to beginning of file.
    • feof(): Checks if end-of-file has been read.

Rewind

  • Purpose: Resets the file pointer without disconnecting the file.

End of File

  • Functionality: feof() returns true if end-of-file mark was read.

Comparison of File I/O and Standard I/O Functions

  • Similarities in function prototypes and return types between file I/O and standard I/O functions.
  • Important to check return values, especially for EOF indications.
Function TypeStandard I/OFile I/ONotes
intscanf(...)fscanf(fp, ...)Check for EOF
intprintf(...)fprintf(fp, ...)Returns number of characters written
intgetchar()fgetc(fp)Check for EOF before converting
intputchar(ch)fputc(ch, fp)Check for EOF