Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

/* 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 id[4]; char name[32];

} 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); //ERROR fwrite(list, sizeof(RECORD), 5, 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); // ERROR fwrite(&item, sizeof(RECORD), 1, fp);

// calculate the number of records in the file fseek(fp, 0, SEEK_END); pos = ftell(fp); //printf("bytes %ld", pos/ sizeof(RECORD)); 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; }

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_2

Step: 3

blur-text-image_3

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

Making Databases Work The Pragmatic Wisdom Of Michael Stonebraker

Authors: Michael L. Brodie

1st Edition

1947487167, 978-1947487161

More Books

Students also viewed these Databases questions

Question

2. What should an employer do when facing an OSHA inspection?

Answered: 1 week ago