Systems IO
Chunk Copy
A small buffer can copy a file in chunks until fread reaches the end.
chunk
Each loop copies only the bytes that were actually read.
total bytes
Counting copied bytes verifies how much data moved through the buffer.
Chunk Copy
chunk_copy.c
Replay: real traced execution (multi-file project)
#include <stdio.h>
int main(void) {
int take = 5;
char input[6] = {'a', 'b', 'c', 'd', 'e', 'f'};
char buffer[3];
int total = 0;
FILE *out = fopen("egtry_c17_source.bin", "wb");
fwrite(input, sizeof(input[0]), take, out);
fclose(out);
FILE *in = fopen("egtry_c17_source.bin", "rb");
int read = (int)fread(buffer, sizeof(buffer[0]), 3, in);
while (read > 0) {
total = total + read;
read = (int)fread(buffer, sizeof(buffer[0]), 3, in);
}
fclose(in);
remove("egtry_c17_source.bin");
printf("copied=%d\n", total);
return 0;
}
#include <stdio.h>
int main(void) {
int take = 3;
char input[6] = {'a', 'b', 'c', 'd', 'e', 'f'};
char buffer[3];
int total = 0;
FILE *out = fopen("egtry_c17_source.bin", "wb");
fwrite(input, sizeof(input[0]), take, out);
fclose(out);
FILE *in = fopen("egtry_c17_source.bin", "rb");
int read = (int)fread(buffer, sizeof(buffer[0]), 3, in);
while (read > 0) {
total = total + read;
read = (int)fread(buffer, sizeof(buffer[0]), 3, in);
}
fclose(in);
remove("egtry_c17_source.bin");
printf("copied=%d\n", total);
return 0;
}
#include <stdio.h>
int main(void) {
int take = 6;
char input[6] = {'a', 'b', 'c', 'd', 'e', 'f'};
char buffer[3];
int total = 0;
FILE *out = fopen("egtry_c17_source.bin", "wb");
fwrite(input, sizeof(input[0]), take, out);
fclose(out);
FILE *in = fopen("egtry_c17_source.bin", "rb");
int read = (int)fread(buffer, sizeof(buffer[0]), 3, in);
while (read > 0) {
total = total + read;
read = (int)fread(buffer, sizeof(buffer[0]), 3, in);
}
fclose(in);
remove("egtry_c17_source.bin");
printf("copied=%d\n", total);
return 0;
}
take ← 5, input ← abcdef, buffer ← q, total ← 0, out ← ⟨addr A⟩
3int main(void) {4 int take→ 5 = 5; //@take=3, 65 char input→ abcdef[6] = {'a', 'b', 'c', 'd', 'e', 'f'};6 char buffer→ q[3];7 int total→ 0 = 0;89 FILE *out→ ⟨addr A⟩ = fopen("egtry_c17_source.bin", "wb");10 fwrite(inputabcdef, sizeof(input[0]), take5, out⟨addr A⟩);11 fclose(out⟨addr A⟩);1213 FILE *in→ ⟨addr A⟩ = fopen("egtry_c17_source.bin", "rb");14 int read→ 3 = (int)fread(buffer→ abc�k��, sizeof(buffer[0]), 3, in⟨addr A⟩);15 while (read > 0) {total ← 3, read ← 2, buffer ← dec�k��
pass 1 of 214int read = (int)fread(buffer, sizeof(buffer[0]), 3, in);15while (read3 > 0) {16 total→ 3 = total + read3;17 read→ 2 = (int)fread(buffer→ dec�k��, sizeof(buffer[0]), 3, in⟨addr A⟩);18}total ← 5, read ← 0
pass 2 of 214int read = (int)fread(buffer, sizeof(buffer[0]), 3, in);15while (read2 > 0) {16 total→ 5 = total + read2;17 read→ 0 = (int)fread(bufferdec�k��, sizeof(buffer[0]), 3, in⟨addr A⟩);18}fclose(in);
18 }19 fclose(in⟨addr A⟩);20 remove("egtry_c17_source.bin");2122 printf("copied=%d\n", total5);23 return 0;24}outputcopied=5
take ← 3, input ← abcdef, buffer ← s, total ← 0, out ← ⟨addr A⟩
3int main(void) {4 int take→ 3 = 3;5 char input→ abcdef[6] = {'a', 'b', 'c', 'd', 'e', 'f'};6 char buffer→ s[3];7 int total→ 0 = 0;89 FILE *out→ ⟨addr A⟩ = fopen("egtry_c17_source.bin", "wb");10 fwrite(inputabcdef, sizeof(input[0]), take3, out⟨addr A⟩);11 fclose(out⟨addr A⟩);1213 FILE *in→ ⟨addr A⟩ = fopen("egtry_c17_source.bin", "rb");14 int read→ 3 = (int)fread(buffer→ abc����, sizeof(buffer[0]), 3, in⟨addr A⟩);15 while (read > 0) {total ← 3, read ← 0
14int read = (int)fread(buffer, sizeof(buffer[0]), 3, in);15while (read3 > 0) {16 total→ 3 = total + read3;17 read→ 0 = (int)fread(bufferabc����, sizeof(buffer[0]), 3, in⟨addr A⟩);18}fclose(in);
18 }19 fclose(in⟨addr A⟩);20 remove("egtry_c17_source.bin");2122 printf("copied=%d\n", total3);23 return 0;24}outputcopied=3
take ← 6, input ← abcdef, buffer ← |, total ← 0, out ← ⟨addr A⟩
3int main(void) {4 int take→ 6 = 6;5 char input→ abcdef[6] = {'a', 'b', 'c', 'd', 'e', 'f'};6 char buffer→ |[3];7 int total→ 0 = 0;89 FILE *out→ ⟨addr A⟩ = fopen("egtry_c17_source.bin", "wb");10 fwrite(inputabcdef, sizeof(input[0]), take6, out⟨addr A⟩);11 fclose(out⟨addr A⟩);1213 FILE *in→ ⟨addr A⟩ = fopen("egtry_c17_source.bin", "rb");14 int read→ 3 = (int)fread(buffer→ abc�bu�, sizeof(buffer[0]), 3, in⟨addr A⟩);15 while (read > 0) {total ← 3, buffer ← def�bu�
pass 1 of 214int read = (int)fread(buffer, sizeof(buffer[0]), 3, in);15while (read3 > 0) {16 total→ 3 = total + read3;17 read3 = (int)fread(buffer→ def�bu�, sizeof(buffer[0]), 3, in⟨addr A⟩);18}total ← 6, read ← 0
pass 2 of 214int read = (int)fread(buffer, sizeof(buffer[0]), 3, in);15while (read3 > 0) {16 total→ 6 = total + read3;17 read→ 0 = (int)fread(bufferdef�bu�, sizeof(buffer[0]), 3, in⟨addr A⟩);18}fclose(in);
18 }19 fclose(in⟨addr A⟩);20 remove("egtry_c17_source.bin");2122 printf("copied=%d\n", total6);23 return 0;24}outputcopied=6
Exercise: chunk_copy.c
Copy only the bytes returned by each read and report a short final chunk correctly