Activity - File I/O
In this activity, we loop though a given list of file names as command-line arguments and output the first line of each file to the screen.
- Obtain a copy of the template C program, fileIO-template.c:
cd ~/cs50
cp /thayerfs/courses/22spring/cosc050/workspace/fileIO-template.c fileIO.c
- Fill in the loop body so that it attempts to open the file given by each argument, and prints the first line of each file.
If a file cannot be opened (e.g., this file does not exist, or you do not have permission to open it), move on to the next file name. Make the return value of the program the number of files that it fails to open.
An example of running it with three file names, where joke file does not exist:
$ ./fileIO labs/lab1-xia-z/query.sh joke labs/lab1-xia-z/README.md
labs/lab1-xia-z/query.sh: #!/bin/bash
failed to open joke
labs/lab1-xia-z/README.md: # CS50 Spring 2022, Lab 1
- Solution: fileIO.c