CSE 109 Final Examination FALL 2005

CSE 109 Final Examination FALL 2005 - CSE 109 Final...

Info iconThis 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); 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);
Background image of page 1

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

View Full DocumentRight Arrow Icon
int main() {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); } void check(int b, char *mess) {if(!b) {fprintf(stderr,"ERROR: %s\n",mess);
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

CSE 109 Final Examination FALL 2005 - CSE 109 Final...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online