Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

CS221 Data Structures

Assignment 01: Introduction to File IO and struct data


type.
Due : 23h55 Tuesday 16 March, 2021.
FCSE
GIKI
Fall 2021

1 Objectives
(a) Test the student’s ability to use File IO System Calls.
(b) Test the student’s ability to use the struct data type.
(c) Test the student’s ability work with dynamic memory allocation.

(d) Test the student’s ability do error handling in code.


(e) Test the student’s ability to follow written instructions.

2 Description
You will be using the C programming language to do this assignment. You will need to use C
functions related to string manipulation, taking input from user, and using the C library built-in
functions for File IO.

3 Task 1
T1. Display the contents of a file.
You will write a program that the user can run with command line arguments. When running
your program the user can optionally give it a maximum of two command line arguments.

1. the first argument will be a file path


2. the second option is optional and will be a string ”-n”

You program shall open the file whose path the user has passed via the command line and
read its contents line by line. It shall display these contents on the standard output.

If the user provides the second argument -n on the command line, the behaviour of your
program should slightly change and when displaying the contents of the file on standard output
line by line, it should prepend each line with a line number. The line numbers will start from 1.

1
4 Task 2
T2. Display random data from a file.
In this task you will write a program that will take at most two arguments from the command
line:

1. the first argument will be a file path


2. the second option is optional and will be a positive integer

Your program shall open and read the file whose path is provided on the command line as
the first argument. The file will contain student data in the following format:

N
reg_no_1 name_1
reg_no_2 name_2
....
reg_no_N name_N
The first line will contain a positive integer N telling you how many lines are going to follow.
The next N lines will contain the data for N students. Each line will first contain the student’s
registration number followed by their name. The name can have spaces between the first, middle
and last part of the name.

In you program you will declare a struct to contain the registration number and full name of
each student.

After reading the positive integer on the first line, you will allocate memory , dynamically,
for N such structs. Then you will fill these structs with the user data read from the file.

The second argument is optional and will be a positive integer, let’s say p. When given,
your program should randomly pick p students among the list and display their data on the
screen preceded by a serial number starting from 1. If the second argument is not provided, your
program should randomly select and display data for one student only.

5 Error Checking and Clean Code instructions


It is extremely important that your program handles errors. You shall always insert code for error
checking where ever there is a possibility of things going wrong. In case of any errors, the pro-
gram should output a helpful error message. All error messages should be displayed on stderr. 1

Things that can go wrong may include, but are not limited to, :
• invalid number of command line arguments
• file path is not valid, i.e., file does not exist
• being passed a -ve value where a +ve one is expected

• failure to allocate memory


• failure to open(), read(), or write() file
1 You might also want to read about the use of errno, perror(), and strerror().

2
• invalid data input
You would lose marks if your program crashes during use.

It is the programmer’s responsibility to free any system resources i.e. memory , file de-
scriptors, etc., they have acquired from the system. Your program should always free system
resources once it is done using them.

Code should be properly indented, readable and commented.

You should always your compile your code using the -Wall option to check for warnings.
Your program should compile without any warnings.

If your program does not compile you will get a 0.

If your program has memory leaks, you’ll lose half the marks in that task.

6 Submission Instructions
1. You submission should consist of .c files only.
2. Each of you will submit two .c files which you shall name as u2019xxx a1 t1.c and u2019xxx a1 t2.c
where xxx are the last three digits of your registration number.
3. You will submit on MS Teams or CMS. We’ll update you which to use before the deadline.
4. Missing submission deadline on will cost you 20 marks (50%). Submissions received more
than 24 hours after submission deadline will get a 0.

7 Rubric
This is an individual assignment. Any form of collaboration, cheating, plagiarism will get
you a 0. Giving your code to somebody else, even if it is for their understanding only, is not
allowed. You may be called for a viva; if you are unable to explain any line of the submitted
code, you’ll get a 0.

Any form of plagiarism or collusion will get you at least a 0 in the assignment and, poten-
tially, an F in the course.

To discourage plagiarism and encourage academic honesty, if you’ve been unable to do any
thing you can submit a program saying Hello World before the deadline by following submission
instructions (name your file u2019xxx a01 hw.c), and get the submission marks. This way you
are sure to get at least 25% of the marks.

Category Marks
Followed submission insns 10 marks
Code was readable +
Compiled without warnings +
Does not crash +
Program handles errors well 10 marks
T1 working properly 10 marks
T2 working properly 10 marks
Total 40 marks

You might also like