This is a modified version of the lab on the IPC144 page.
In this lab, you will work with files. If you have problems, ask for help - that is what the lab sessions are for.
NOTE: When you have completed this lab, and placed answers into the file called "lab11.txt", follow the instructions (at the bottom of this lab) to submit your answers to Murray Saul's e-mail account...
Enter the following program and call it lab11a.c
#include <stdio.h>
main() {
FILE *fp;
char line[81], filename[81];
printf("Enter a file name: ");
gets(filename);
fp = fopen(filename, "a");
if (fp == NULL)
printf("Cannot open %s\n", filename);
else {
do {
printf("Enter a line: ");
gets(line);
if (line[0] != '\0')
fprintf(fp,"%s\n", line);
} while (line[0] != '\0');
fclose(fp);
}
}
102;Hyper VGA board;59.95and enter the following program and call it lab11b.c
213;40 GB Hard Drive;239.99
73;P4 motherboard;199.00
#include <stdio.h>This program looks up part numbers in the lab11.dat file. Try the program out, entering part numbers which are on file (102, 213 and 73), and some which aren't. Be sure that you understand how the program works. For example, can you explain the following?
#include <string.h>
void locate (FILE *fp, int partnum, char desc[], double *pprice) {
int pnum,
varsread,
found = 0;
rewind(fp);
varsread = fscanf(fp, "%d;%30[^;];%lf%*c", &pnum, desc, pprice);
while (found == 0 && 3 == varsread)
if (pnum == partnum)
found = 1;
else
varsread = fscanf(fp, "%d;%30[^;];%lf%*c", &pnum, desc,
pprice);
if (found == 0) {
strcpy(desc, "Part Not Found");
*pprice = 0;
}
}
main() {
FILE *fp;
int part;
char desc[31];
double price;
fp = fopen("lab11.dat", "r");
if (fp == NULL)
printf("Cannot open lab11.dat\n");
else {
do {
printf("Enter a part number (0 to stop): ");
scanf("%d", &part);
if (part != 0) {
locate(fp, part, desc, &price);
printf("Description: %s, Price: $%.2lf\n", desc, price);
}
} while (part != 0);
fclose(fp);
}
}
int load(FILE *fp, int pnums[], char descs[][31], double prices[]) {and modify main so that it looks like this:
int i = 0;
while (3 == fscanf(fp, "%d;%30[^;];%lf%*c", &pnums[i], descs[i],
&prices[i]))
i++;
return i;
}
void locate2(int pnum, char desc[], double *pprice, int pnums[],
char descs[][31], double prices[], int count) {
int i = 0;
while (i < count && pnum != pnums[i])
i++;
if (i == count) {
strcpy(desc, "Part Not Found");
*pprice = 0;
} else {
strcpy(desc, descs[i]);
*pprice = prices[i];
}
}
main() {Satisfy yourself that this works the same as the program in lab11b.c.
FILE *fp;
int part, parts[40], count;
char desc[31], descs[40][31];
double price, prices[40];
fp = fopen("lab11.dat", "r");
if (fp == NULL)
printf("Cannot open lab11.dat\n");
else {
count = load(fp, parts, descs, prices);
fclose(fp);
do {
printf("Enter a part number (0 to stop): ");
scanf("%d", &part);
if (part != 0) {
locate2(part, desc, &price, parts, descs, prices, count);
printf("Description: %s, Price: $%.2lf\n", desc, price);
}
} while (part != 0);
}
}
________________________________________________________________________________________________________
What could be the disadvantages?
________________________________________________________________________________________________________
Submission Requirements:
If you are in Murray Saul's class, issue the following command to send your lab #5 answers to Murray Saul:
mail -s "144lab11" -c $USER@learn.senecac.on.ca murray.saul@senecac.on.ca < lab11.txt
The option -s "144lab11" makes subject line appear as "144lab11" so instructor can filter these e-mails in a directory to collect all lab11 submissions.
The option -c $USER@learn.senecac.on.ca sends a copy of the e-mail message to YOUR learn account. The variable $USER is your Matrix id name assuming that you are issuing this command when logged into your Matrix account. Please keep this e-mail for the remainder of this term as proof that you sent your lab by the required deadline...