Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C Programming Language Binary Files There are a number of errors (about 6) in the following program. Locate all errors, fix them (as shown below),

C Programming Language Binary Files There are a number of errors (about 6) in the following program. Locate all errors, fix them (as shown below), run the program and save its output. Here is an example: FILE fp; // <== Error: Comment the line and write the correct line below // FILE fp; // Error #1 FILE *fp; #include #include // exit #include #include

#define FILENAME "out.dat"

#define SHOW_RECORDS(x) rewind(fp); \ fread(temp, sizeof(RECORD), x, fp); \ for (k = 0; k < x; k++ ) printf("%s %s ", temp[k].id, temp[k].name); \ rewind(fp);

typedef struct { char name[32]; char id[4]; } RECORD;

int main(void) { FILE *fp; RECORD list[5] = {{"1111", "Alpha"}, {"2222", "Beta"}, {"3333", "Gamma"}, {"4444", "Delta"}, {"5555","Epsilon"}}; RECORD myList[3] = {{"7777", "Eta"}, {"8888", "Theta"}, {"9999", "Iota"}}; RECORD temp[5], item = {"6666", "Zeta"}, newItem; long pos, cnt; int k, n = 5; if ((fp = fopen(FILENAME, "w+b")) == NULL) { printf("Could not open %s. ", FILENAME); exit(1); } fwrite(list, 5, sizeof(RECORD), fp); SHOW_RECORDS(5); // calculate the number of records in the file fseek(fp, 0, SEEK_END); pos = ftell(fp); printf(" There are %ld records in this file. ", pos/sizeof(RECORD) ); // append one item fseek(fp, 0, SEEK_END); fwrite(item, sizeof(RECORD), 1, fp); // calculate the number of records in the file fseek(fp, 0, SEEK_END); pos = ftell(fp); printf(" Now there are %ld records in this file. ", pos/sizeof(RECORD) ); // Display the last record fseek(fp, -1 * sizeof(RECORD), SEEK_END); fread(&newItem, sizeof(RECORD), 1, fp); printf(" Last item is %s. ", newItem.name); // append myList fseek(fp, 0, SEEK_END); fwrite(myList, sizeof(RECORD), 3, fp); // calculate the number of records in the file fseek(fp, 0, SEEK_END); pos = ftell(fp); cnt = pos/sizeof("RECORD"); printf(" There are %ld records in this file. ", cnt ); SHOW_RECORDS(cnt); // replace the n-th element by {"0000", "ZERO"} // newItem fseek(fp, n * sizeof(RECORD), SEEK_SET); // strcpy(item.id, "0000"); strcpy(item.name, "ZERO"); fwrite(&item, sizeof(RECORD), 1, fp); fseek(fp, -1 * sizeof(RECORD), SEEK_END); fread(&newItem, sizeof(RECORD), 1, fp); printf(" The %d-th record has changed: %s %s. ", n, newItem.id, newItem.name); SHOW_RECORDS(cnt); fclose(fp); return 0; }

/* ================= Sample Output ================= */ /* Results: */

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Medical Image Databases

Authors: Stephen T.C. Wong

1st Edition

1461375398, 978-1461375395

More Books

Students also viewed these Databases questions