Lecture 25, ECE264, Spring 2012, Prof. Lu


Shiyu Wang Lec25 April 14th

Exam average is around 5-6

for text file

fscanf(filename,"%d",&val)

for binary file

fread(&val, sizeof(int), 1, filename)

===================

Kevin Tan(0023987592), section#2 notes 04/12

EXAM Q2 Thing thing_construct(int w,char *l) {

 Thing t;
 t.label = strdup(1);
 t.weight = w;
 return t;
 

}

Thing Thing_copy(Thing p) {

 return thing_construct(P.weight,P.label)

}

void Thing_merge(Thing *dst,Thing src) {

 char *l;
 dst->weight = src.weight;
 char *l1 = dst->label;
 char *l2= src.label;
 l= malloc(sizeof(char)*(strlen(l1)+strlen(l2)+1));
 strcpy(l,l1);
 strcpy(l,l2);
 free(dat->label);
 dst->label = 1;
 

}

void Thing_destory(Thing p) {

 free(p.label);

}

void Thing_print(Thing p) {

 printf("weight = %d, label = %s\n",p.weight,p.label);

}

==========================

Hanye Xu April 17

For exam2

fscanf(filename,"%d",&val) fread(&val, sizeof(int), 1, filename)

for the first Question

For the Q2:

void Thing_merge(Thing *dst,Thing src) {

 char *l;
 dst->weight = src.weight;
 char *l1 = dst->label;
 char *l2= src.label;
 l= malloc(sizeof(char)*(strlen(l1)+strlen(l2)+1));
 strcpy(l,l1);
 strcpy(l,l2);
 free(dat->label);
 dst->label = 1;
 

}

======================================

April 19th, Wudi Zhou, Prof. Lu's section

Q1:

textfile = fopen(argc[1],"r"); while(fscanf(textfile,"%d",&temp) != EOF) { sum = sum + temp; } binarayfile = fopen(argv[2],"rb"); while(fread(&temp,sizeof(int),1,binaryfile) != 0) { sum = sum + temp; }

output = fopen(argv[3],"w"); fpeintf(output,"%d\n",sum);

Q2:

Thing Thing_construct(int w, char * l) { Thing thing; thing.label = malloc(sizeof(char) * strlen(l) + 1)); thing.weight = w; strcpy(thing.label,l); return thing; }

Thing Thing_copy(Thing p) { Thing copy; copy = Thing_construct(p.weight,p.label); return copy; }

void Thing_merge(THing *dst, Thing src) { dsr -> weight = dst-> weight + src.weight; char * p = dst->label; dst->label = malloc(sizeof(char) * (strlen(dst->label) + 1 + strlen(src.label)); strcpy(dst->label,p); free(p); dst -> label = srecat(dst -> label,src.label);

return; }

============

Rohan Khante

============

Q1:

textfile = fopen(argc[1],"r");

while(fscanf(textfile,"%d",&temp) != EOF) //note the method of declaring EOF

{

sum = sum + temp;

}

binarayfile = fopen(argv[2],"rb");

while(fread(&temp,sizeof(int),1,binaryfile) != 0)

{

sum = sum + temp;

}

output = fopen(argv[3],"w");

fprintf(output,"%d\n",sum);

COMPARISON fscanf(textfile,"%d",&temp) != EOF fread(&temp,sizeof(int),1,binaryfile) != 0)

FSCANF- first the filename comes. This line means keep executing till EOF is not reached FREAD- variable followed size of the variable type. the pointer is then advanced by the number of bytes being read. This line in all means that as long as you keep getting numbers do not stop



EXAM Q2


void Thing_merge(Thing *dst,Thing src) {

char *l;
dst->weight = src.weight;
char *l1 = dst->label;
char *l2= src.label;
l= malloc(sizeof(char)*(strlen(l1)+strlen(l2)+1));
strcpy(l,l1);
strcpy(l,l2);
free(dst->label);
dst->label = 1;

}

Thing_merge deserves a special mention since all the other functions were coverd by the Professor in class. In destination ->weight the weight of src is stored. The reason we use -> for destination and '.' for src is because a pointer to dst is passed. ->(Pointer) l1 and l2 are made in which the labels of dst and src are stored. mallocing is done for strlen(l1)+strlen(l2)+1 because you want the size of l1 and l2 AND the termination character '\0'. In the end the dst->label is freed


Back to ECE264, Spring 2012, Prof. Lu

Alumni Liaison

Questions/answers with a recent ECE grad

Ryne Rayburn