Files
Write And Read
Opening a file with w+ lets the program write content, rewind, and read it back.
write mode
`w+` creates a writable and readable file for this small example.
rewind
After writing, `rewind` moves the stream position back to the beginning.
Write And Read
write_read.c
Replay: real traced execution (multi-file project)
#include <stdio.h>
int main(void) {
int count = 2;
FILE *file = fopen("write_read_demo.txt", "w+");
if (file == 0) {
return 1;
}
for (int i = 1; i <= count; i++) {
fprintf(file, "%d\n", i);
}
rewind(file);
int lines = 0;
int value = 0;
while (fscanf(file, "%d", &value) == 1) {
lines++;
}
fclose(file);
remove("write_read_demo.txt");
printf("lines=%d\n", lines);
return 0;
}
#include <stdio.h>
int main(void) {
int count = 3;
FILE *file = fopen("write_read_demo.txt", "w+");
if (file == 0) {
return 1;
}
for (int i = 1; i <= count; i++) {
fprintf(file, "%d\n", i);
}
rewind(file);
int lines = 0;
int value = 0;
while (fscanf(file, "%d", &value) == 1) {
lines++;
}
fclose(file);
remove("write_read_demo.txt");
printf("lines=%d\n", lines);
return 0;
}
#include <stdio.h>
int main(void) {
int count = 4;
FILE *file = fopen("write_read_demo.txt", "w+");
if (file == 0) {
return 1;
}
for (int i = 1; i <= count; i++) {
fprintf(file, "%d\n", i);
}
rewind(file);
int lines = 0;
int value = 0;
while (fscanf(file, "%d", &value) == 1) {
lines++;
}
fclose(file);
remove("write_read_demo.txt");
printf("lines=%d\n", lines);
return 0;
}
count ← 2, file ← ⟨addr A⟩
3int main(void) {4 int count→ 2 = 2; //@count=3, 45 FILE *file→ ⟨addr A⟩ = fopen("write_read_demo.txt", "w+");for (int i = 1; i <= count; i++)
pass 1 of 211for (int i1 = 1; i <= count2; i++) {12 fprintf(file⟨addr A⟩, "%d\n", i1);13}for (int i = 1; i <= count; i++)
pass 2 of 211for (int i2 = 1; i <= count2; i++) {12 fprintf(file⟨addr A⟩, "%d\n", i2);13}lines ← 0, value ← 0
15rewind(file⟨addr A⟩);1617int lines→ 0 = 0;18int value→ 0 = 0;19while (fscanf(file, "%d", &value) == 1) {lines ← 1
pass 1 of 218int value = 0;19while (fscanf(file⟨addr A⟩, "%d", &value1) == 1) {20 lines→ 1++;21}lines ← 2
pass 2 of 218int value = 0;19while (fscanf(file⟨addr A⟩, "%d", &value2) == 1) {20 lines→ 2++;21}fclose(file);
23 fclose(file⟨addr A⟩);24 remove("write_read_demo.txt");2526 printf("lines=%d\n", lines2);27 return 0;28}outputlines=2
count ← 3, file ← ⟨addr A⟩
3int main(void) {4 int count→ 3 = 3;5 FILE *file→ ⟨addr A⟩ = fopen("write_read_demo.txt", "w+");for (int i = 1; i <= count; i++)
pass 1 of 311for (int i1 = 1; i <= count3; i++) {12 fprintf(file⟨addr A⟩, "%d\n", i1);13}All 3 passes — pass 1 is the card above pass i1 1 2 2 3 3 lines ← 0, value ← 0
15rewind(file⟨addr A⟩);1617int lines→ 0 = 0;18int value→ 0 = 0;19while (fscanf(file, "%d", &value) == 1) {lines ← 1
pass 1 of 318int value = 0;19while (fscanf(file⟨addr A⟩, "%d", &value1) == 1) {20 lines→ 1++;21}All 3 passes — pass 1 is the card above pass valuelines1 1 0 → 1 2 2 1 → 2 3 3 2 → 3 fclose(file);
23 fclose(file⟨addr A⟩);24 remove("write_read_demo.txt");2526 printf("lines=%d\n", lines3);27 return 0;28}outputlines=3
count ← 4, file ← ⟨addr A⟩
3int main(void) {4 int count→ 4 = 4;5 FILE *file→ ⟨addr A⟩ = fopen("write_read_demo.txt", "w+");for (int i = 1; i <= count; i++)
pass 1 of 411for (int i1 = 1; i <= count4; i++) {12 fprintf(file⟨addr A⟩, "%d\n", i1);13}All 4 passes — pass 1 is the card above pass i1 1 2 2 3 3 4 4 lines ← 0, value ← 0
15rewind(file⟨addr A⟩);1617int lines→ 0 = 0;18int value→ 0 = 0;19while (fscanf(file, "%d", &value) == 1) {lines ← 1
pass 1 of 418int value = 0;19while (fscanf(file⟨addr A⟩, "%d", &value1) == 1) {20 lines→ 1++;21}All 4 passes — pass 1 is the card above pass valuelines1 1 0 → 1 2 2 1 → 2 3 3 2 → 3 4 4 3 → 4 fclose(file);
23 fclose(file⟨addr A⟩);24 remove("write_read_demo.txt");2526 printf("lines=%d\n", lines4);27 return 0;28}outputlines=4