c109f05f - CSE 109 Final Examination Thursday 15 December...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 109 Final Examination Thursday 15 December 2005 <<<<<<<<<<<<<<<<<<<<<<<<<<<<SUGGESTED ANSWERS>>>>>>>>>>>>>>>>>>>>>>>>>> 1. Write a C program (compiled with "gcc -xc") similar to the unix "cp" command. In particular, it should take (demand) two arguments, the first the name of the (text) file to be copied, the second the name of the file to contain the copy. For example, if the executable file is stored in "a.out" then the command a.out x y should create in the file 'y' a copy of the file 'x', provided the file 'x' exists. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< #include <stdio.h> # void check(int b,char *mess,char *m1, char *m2); v int main(int ct,char **arg) {FILE *fin,*fout; char ch; check(ct==3,"Usage: ",arg[0]," <input> <output>"); fin=fopen(arg[1],"r"); check(fin!=NULL,"Failure to open the input file '",arg[1],"'"); fout=fopen(arg[2],"w"); check(fout!=NULL,"Failure to open the output file '",arg[2],"'"); ch=getc(fin); while(ch!=EOF) {putc(ch,fout); ch=getc(fin); } fclose(fout); } void check(int b,char *mess,char *m1, char *m2) {if(!b) {printf("ERROR: %s%s%s\n",mess,m1,m2); exit(1); } } >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 2. Assume that the binary file 'final.dat' contains 10 blocks of data, with each block created from the struct below. Write a C program (compiled with "gcc -xc") that prompts the user for the number of a block and responds by displaying the contents of the struct stored at that block, if the block number is legitimate, where the blocks are numbered 0, 1, 2, ..., 9. struct ABlock {int a; double b; } <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< #include <stdio.h> # struct ABlock {int a; double b; }; } void check(int b,char *mess); v int main()
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
{FILE *fin; struct ABlock dat; int block; fin=fopen("final.dat","rb"); check(fin!=NULL,"Failure to open 'final.dat'"); printf("Enter the number of the block- "); scanf("%d",&block); check(block>=0 && block<=9,"Bad block number entered"); fseek(fin,block*sizeof(struct ABlock),SEEK_SET); fread(&dat,sizeof(struct ABlock),1,fin); printf(" a = %d, b = %f\n",dat.a,dat.b);
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern