C programming to read and sort a file

timer Asked: Jan 26th, 2014

Question description


Write a C program in Linux that will read from an input file a sequence of student records, will

sort them, and will print the sorted records into an output file. The input file will be a text file

containing information about one student per line. Each line will include the following information

in the following format:

StudentID Firstname Lastname Department GPA

You can assume each of the above information (Firstname, Department, etc.) will be a single

word. Assume StudentID is a 7 digit number between (and including) 1000000 and 9999999. Assume

GPA is a floating point number between (and including) 0 and 4. There can be one or more space or

TAB characters between the fields. If you use the fscanf() function, then this will not matter and

you will be able to read the fields of a line easily into your variables. Note that Firstname, Lastname,

and Department can be any length, therefore make sure you do not make any assumption about their

lengths and create the memory required to hold them dynamically (Tip: the m modifier of fscanf()

might be handy here).

Your program will read the information from the input file and will build a linked list where each

item (entry) of the list will contain information about one student. Hence each item of the linked list

will be a structure having fields to store information about a student (in C we do not have class, we

have struct). You can build the list as a doubly linked list. In this case, each item in the list will

have also a pointer to the next item and a pointer to the previous item.

After building the list, your program will sort the list using the insertion sort algorithm (Tip: You

can build the list in the sorted order as well.). Sorting will be done based on the StudentID field.

Then your program will write the list of items into an output text file. Each line of the output file will

contain information about one student in the following format:


Note the comma (instead of space or TAB characters) between fields. The output should contain

no spaces or TABs, and no empty lines. So, after you write the last line of information, you should

close the file. And this should be the end of your program. It is very important that you produce the

output in this format since we will use this format in our automated tests.


It is a requirement that you have to use linked list data structure and insertion sort algorithm. In this

way, you will be able to practice with pointers and structures.

The name of your executable file has to be unisort. A sample invocation of your program can

be like the following:

unisort in.txt out.txt

Here, in.txt is the input text file, and out.txt is the output text file. It is very important that

you follow the specifications.

Example 1 An example input file (for example, in.txt) can be like the following:

2040003 AAAA     BBBBBBBBB  ComputerScience 3.45

2040002   AAA CCC          ElectricalEngineering        3.01

2040005 AAAAAAAAAAAAAAAAA BBB ComputerScience 3.60

Then the output file (for example, out.txt) will be the following:




Tutor Answer

(Top Tutor) Studypool Tutor
School: UC Berkeley
Studypool has helped 1,244,100 students
flag Report DMCA
Similar Questions
Hot Questions
Related Tags

Brown University

1271 Tutors

California Institute of Technology

2131 Tutors

Carnegie Mellon University

982 Tutors

Columbia University

1256 Tutors

Dartmouth University

2113 Tutors

Emory University

2279 Tutors

Harvard University

599 Tutors

Massachusetts Institute of Technology

2319 Tutors

New York University

1645 Tutors

Notre Dam University

1911 Tutors

Oklahoma University

2122 Tutors

Pennsylvania State University

932 Tutors

Princeton University

1211 Tutors

Stanford University

983 Tutors

University of California

1282 Tutors

Oxford University

123 Tutors

Yale University

2325 Tutors