Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Reading

(1-2h) Study Chapter 11 including exercise 10.4 (p. 570) with the learning goals in mind. The following section/s is/are optional:

In the following you also find my own notes.

Learning goals

11.1 Introduction

  • file for long-term data retention, RAM vs secondary storage, e.g., solid state drive

  • sequential- and random- access will be discussed

Sequential access is used on magnetic tapes, which are still in use. Modern drives support random and thus also sequential access.

Sequential access is used on magnetic tapes, which are still in use. Modern drives support random and thus also sequential access.

11.2 Files and Streams

Standard streams

We used these streams with scanf and printf:

stderr is new. Is used to distinguish between actual data and diagnostics data[1]

FILE structure

defined in <stdio.h>. FILE Includes

Each array element contains:

Relation between a stream (stdout etc) and FILE

After a file is opened with fopen, the program accesses it through a FILE * stream.

Functions

Basic:

  • fgetc

  • fputc

String:

  • fgets

  • fputs

Formatted:

  • fscanf

  • fprintf

Binary (compared to text):

  • fread

  • fwrite

11.3 Creating a sequential-access file

Written to file.
FILE contains a file descriptor, which is tied to a file table entry in the operating system (OS). File table and inode table are part of the OS. OS helps the processes to coordinate concurrent read/write operations. 
Qwertyus, CC-BY SA 4.0, Source

FILE contains a file descriptor, which is tied to a file table entry in the operating system (OS). File table and inode table are part of the OS. OS helps the processes to coordinate concurrent read/write operations.
Qwertyus, CC-BY SA 4.0, Source

Opening Modes

Text and binary modes behave the same on Linux and MacOS

On Windows, text mode translates \r\n to \n on read and \n to \r\n on write.

This applies to C, but not Python, for example. In Python there is a difference between text and binary modes on all platforms. For example:

text_vs_binary_mode.c
#include <stdio.h>

int main() {
  FILE *fp = fopen("data.txt", "w");
  fputs("Line1\n"
        "Line2",
        fp);
  printf("data.txt size: %ld bytes\n", ftell(fp));
  fclose(fp);

  fp = fopen("data.bin", "wb");
  fputs("Line1\n"
        "Line2",
        fp);
  printf("data.bin size: %ld bytes\n", ftell(fp));
  fclose(fp);
}

Output:

On Linux:

data.txt size: 11 bytes
data.bin size: 11 bytes

On Windows:

data.txt size: 12 bytes
data.bin size: 11 bytes

Details: https://stackoverflow.com/a/73572057

Checking file open operation

fopen: No such file or directory

11.4 Reading data from a sequential-access file

file.txt
Hello world!
Hi planet
Hello world!
Hi planet

Reset file position pointer

Updating a sequential file

Requires replacing all the contents after the modification position.

11.5 Random access files

11.6 Creating a random-access file

(int) 0

Writing an array

fwrite’s signature is designed to write arrays:

fwrite(arr, sizeof arr[0], countof arr, fp)

11.7 Random-access write

fseek

After fwrite, assume we want to modify student2

(int) 0

You should see that student2’s data is zeroed.

11.8 Random-access read

Similar, but fread

11.9 Case study

11.10 Secure programming

Footnotes
  1. Unix pipes like, grep geko file.txt > lines_with_geko.txt. If there is an error message by grep, then it is not written to lines_with_geko.txt.

  2. Probably due to legacy, because printf does not add a newline either, even both puts and printf work directly on stdout.