CS 2073 Lab 4: File I/O (Part I) - C Programming Lab, Lab Reports of Computer Science

A lab exercise in c programming for file i/o operations. Students are required to write a program that can read and write data to a text file, implement 'list', 'exit', 'insert', 'delete', and 'update' operations using a switch statement and scanf(). The document also provides a pseudo code for each operation and explains the file format and data structure.

Typology: Lab Reports

Pre 2010

Uploaded on 07/30/2009

koofers-user-exm
koofers-user-exm 🇺🇸

9 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 2073 Lab 4: File I/O (Part I)
Chia-Tien Dan Lo
Department of Computer Science, UTSA
I Objectives
Demonstrate your ability to read/write data from/to a text file
Show the usage of C File I/O routines
II Hand-in Requirements
All laboratories will be submitted electronically through WebCT. Zip up your entire pro ject folder to submit as
the source. (Right click on the laboratory folder and follow the SentTo link.) In this assignment, you only need to
implement the “list” and the “exit” operations. You will complete the whole program in lab 5.
III Details
In this lab, you will be practicing C file I/O routines via an inventory keeping program that can tell you what
products you have and how many you have, and the cost of each. Write a program that reads the file “product.txt”,
let you input the data concerning each product (insert), enables you to list all your product information (list), lets
you delete a record for a product that you no longer have (delete), and lets you update any information in the file
(update). The product identification number should be a unique integer.
Data file format (product.txt, provided at WebCT): This data file is a text file. Therefore, you can examine its
content using programs like “notepad”. Each record (including product ID, name, quantity, and unit cost) takes one
line in the data file.
11111111112222222222333333333344444444445555555555
012345678901234567890123456789012345678901234567890123456789
dddd ssssssssssssssssssss dddd ffffffffff
The first 4 columns is for product ID, followed by a space, followed by 20 columns for product name, followed by
a space, followed by 4 columns for quantity, followed by a space, followed by 10 columns for unit cost, and followed
by a newline character. Pay more attention on the spaces at position 4, 25, 30, and 41. We will use these spaces to
identify fields later.
Since there are four operations (insert, list, delete, and update), you will need to implement a menu and ask for
user’s input:
1. insert
2. list
3. delete
4. update
5. exit
Key in 1-5:
The structure is best implemented using a switch statement and a scanf() to take user’s input. Something like
this:
scanf(‘‘%d’’, &user_input);
switch(user_input) {
case 1:
insert();
break;
1
pf3

Partial preview of the text

Download CS 2073 Lab 4: File I/O (Part I) - C Programming Lab and more Lab Reports Computer Science in PDF only on Docsity!

CS 2073 Lab 4: File I/O (Part I)

Chia-Tien Dan Lo

Department of Computer Science, UTSA

I Objectives

  • Demonstrate your ability to read/write data from/to a text file
  • Show the usage of C File I/O routines

II Hand-in Requirements

All laboratories will be submitted electronically through WebCT. Zip up your entire project folder to submit as the source. (Right click on the laboratory folder and follow the SentTo link.) In this assignment, you only need to implement the “list” and the “exit” operations. You will complete the whole program in lab 5.

III Details

In this lab, you will be practicing C file I/O routines via an inventory keeping program that can tell you what products you have and how many you have, and the cost of each. Write a program that reads the file “product.txt”, let you input the data concerning each product (insert), enables you to list all your product information (list), lets you delete a record for a product that you no longer have (delete), and lets you update any information in the file (update). The product identification number should be a unique integer. Data file format (product.txt, provided at WebCT): This data file is a text file. Therefore, you can examine its content using programs like “notepad”. Each record (including product ID, name, quantity, and unit cost) takes one line in the data file.

11111111112222222222333333333344444444445555555555 012345678901234567890123456789012345678901234567890123456789 dddd ssssssssssssssssssss dddd ffffffffff

The first 4 columns is for product ID, followed by a space, followed by 20 columns for product name, followed by a space, followed by 4 columns for quantity, followed by a space, followed by 10 columns for unit cost, and followed by a newline character. Pay more attention on the spaces at position 4, 25, 30, and 41. We will use these spaces to identify fields later. Since there are four operations (insert, list, delete, and update), you will need to implement a menu and ask for user’s input:

  1. insert
  2. list
  3. delete
  4. update
  5. exit Key in 1-5:

The structure is best implemented using a switch statement and a scanf() to take user’s input. Something like this:

scanf(‘‘%d’’, &user_input); switch(user_input) { case 1: insert(); break;

case 2: list(); break; case 3: delete(); break; case 4: update(); break; case 5: /* close file and exit / Break; default: / error inputs handling */ }

Of course, we like to have a loop for the above structure to let program keep asking for operations to perform. Therefore, the pseudo code is something like:

while (1) { take user input switch(user_input) {...} }

What’s next is implement the four operations: insert(), list(), delete(), and update(). As you have already noticed that each of them will be a C function. Let’s start from list(). It basically reads the data file, and print each line to the console. In order for the list() to perform, we will have to supply a file pointer. Therefore, the prototype of list() should be

void list(FILE *fp)

One easy way to list the data file content is fgets() and puts(). So all together, the pseudo code for the list() function:

void list(FILE fp) { / move the file pointer to the beginning using fseek() */ get a line using fgets() print the line to the console using puts() until end of file is reached }

I add a comment to make sure the file pointer points to the beginning of the file. Otherwise, you may be reading an incomplete file! You can use fseek() to move file pointer. The function update() will modify a line in the data file. So we have to do two things: 1) get the updated information for the record, and 2) overwrite the line in the data file. You can simply use scanf() to take user’s inputs. However, to get a string that contains spaces, you may try gets() instead. The step 2 is a little bit harder. Well, we have to move the file pointer to the beginning of the record first. Since we will need this feature later, let’s just add one more function and call it search(). The algorithm for search() is this:

int search(FILE *fp, long *offset) { while (not EOF) { keep file offset get a line from the data file if the product ID matches then return 0; } return -1; }

A tricky point is that we have to read the line before we figure out if we found the matched record. Once we read the line, the file pointer moves to the end of the line which is the reason that we keep the file offset before we read. The offset will be passing back to the caller. The other thing is about how to get each fields from the line. Remember that the spaces that separate fields! We can simply fill the spaces will null to terminate strings and use atoi(), and atof() to convert data accordingly. So here you go: