There were 10 records stored in “somefile.dat” but the
following program printed 11 names. What went wrong?

void main()

{

struct student

{

char name[30], rollno[6];

}stud;

FILE *fp = fopen(“somefile.dat”,”r”);

while(!feof(fp))

{

fread(&stud, sizeof(stud), 1 , fp);

puts(stud.name);

}

}



There were 10 records stored in “somefile.dat” but the following program printed 11 names. What..

Answer / susie

Explanation:

fread reads 10 records and prints the names successfully. It
will return EOF only when fread tries to read another record
and fails reading EOF (and returning EOF). So it prints the
last record again. After this only the condition feof(fp)
becomes false, hence comes out of the while loop.

Is This Answer Correct ?    2 Yes 4 No

Post New Answer

More C Code Interview Questions

write a c program to Reverse a given string using string function and also without string function

1 Answers  


main() { char s[ ]="man"; int i; for(i=0;s[ i ];i++) printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]); }

1 Answers   DCE,


Cluster head selection in Wireless Sensor Network using C programming language.

0 Answers  


/*what is the output for*/ void main() { int r; printf("Naveen"); r=printf(); getch(); }

4 Answers  


programming in c lanugaue programm will errror error with two header file one as stdio.h and other one is conio.h

1 Answers  






Who could write how to find a prime number in dynamic array?

1 Answers  


Write a routine that prints out a 2-D array in spiral order

3 Answers   Microsoft,


main() { unsigned int i=10; while(i-->=0) printf("%u ",i); }

2 Answers   HP,


main() { char *p; printf("%d %d ",sizeof(*p),sizeof(p)); }

6 Answers  


main() { int i; clrscr(); for(i=0;i<5;i++) { printf("%d\n", 1L << i); } } a. 5, 4, 3, 2, 1 b. 0, 1, 2, 3, 4 c. 0, 1, 2, 4, 8 d. 1, 2, 4, 8, 16

4 Answers   HCL,


Sir... please give some important coding questions asked by product companies..

0 Answers  


void main() { int c; c=printf("Hello world"); printf("\n%d",c); }

2 Answers  


Categories