OlveraeUNSW
Back to problems
COMP1521

COMP1521 2020T2 Q6 — File I/O

COMP1521 2020T2 Q6 · 10 marks · C
Topic: File I/O · Source: https://cgi.cse.unsw.edu.au/~cs1521/26T1/resources/practice_exams.html

Summary: Count the valid ASCII bytes in a file


We need to count the number of ASCII bytes in files.

Write a C program, practice_q6.c ,
which takes a single filename as its argument,
counts the number of bytes in the named file which are valid ASCII,
and prints one line of output containing that count.

Assume a byte is valid ASCII
iff it contains a value between 0 and 127 inclusive.

-
You must match the output format in the example below exactly.

dcc practice_q6.c -o practice_q6
echo hello world >file1
./practice_q6 file1
file1 contains 12 ASCII bytes
echo -e 'hello\xBAworld' >file2
./practice_q6 file2
file2 contains 11 ASCII bytes
echo -n -e '\x80\x81' >file3
./practice_q6 file3
file3 contains 0 ASCII bytes

There is no supplied code for this question.

No error checking is required.

Your program can assume it is always given the name of a file.

Your solution must be in C only.

You are not permitted to run external programs.
You are not permitted to use system, popen, posix_spawn, fork or exec.


🤖 Write a C program. argv[1] is a filename (provided in the run workspace). Count the bytes in that file with value 0..127 and print exactly: <filename> contains <count> ASCII bytes.

Fixture files are provided in the run workspace; reference outputs verified on the sandbox.

Samples

stdin
expected
file1 contains 12 ASCII bytes
stdin
expected
data contains 9 ASCII bytes
c-dcc
14