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

how can i search an element in an array

2 Answers   CTS, Microsoft, ViPrak,


char *someFun1() { char temp[ ] = “string"; return temp; } char *someFun2() { char temp[ ] = {‘s’, ‘t’,’r’,’i’,’n’,’g’}; return temp; } int main() { puts(someFun1()); puts(someFun2()); }

2 Answers  


Is it possible to type a name in command line without ant quotes?

1 Answers   Excel, Infosys,


# include <stdio.h> int one_d[]={1,2,3}; main() { int *ptr; ptr=one_d; ptr+=3; printf("%d",*ptr); }

1 Answers  


prog. to produce 1 2 3 4 5 6 7 8 9 10

4 Answers   TCS,






String reverse with time complexity of n/2 with out using temporary variable.

10 Answers   NetApp, Symantec,


char *someFun() { char *temp = “string constant"; return temp; } int main() { puts(someFun()); }

1 Answers  


What is your nationality?

1 Answers   GoDB Tech,


How will u find whether a linked list has a loop or not?

8 Answers   Microsoft,


posted by surbhi just now main() { float a = 5.375; char *p; int i; p=(char*)&a; for(i=0;i<=3;i++) printf("%02x",(unsigned char) p[i]); } how is the output of this program is :: 0000ac40 please let me know y this output has come

2 Answers   GATE,


Write a program that reads a dynamic array of 40 integers and displays only even integers

2 Answers  


how can u draw a rectangle in C

53 Answers   Accenture, CO, Codeblocks, Cognizant, HCL, Oracle, Punjab National Bank, SAP Labs, TCS, University, Wipro,


Categories