C Programming - Input / Output - Discussion

Discussion Forum : Input / Output - Find Output of Program (Q.No. 12)
12.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    FILE *fp;
    char ch, str[7];
    fp=fopen("try.c", "r"); /* file 'try.c' contains "This is Nagpur" */
    fseek(fp, 9L, SEEK_CUR);
    fgets(str, 5, fp);
    puts(str);
    return 0;
}
agpur
gpur
Nagp
agpu
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
30 comments Page 1 of 3.

Viraj said:   1 decade ago
fgets prototype :char * fgets(char *str,int n,FILE *fp)

fgets reads n-1 characters or till it encounters \n in input whichever comes firsts

Vikas Sharma said:   1 decade ago
Thanks bro!

Gouthami said:   1 decade ago
We want more explanation.

Sai said:   1 decade ago
@viraj.

That is will it take 4 chars when 5 chars are said to be taken in fgets?

Baadal said:   1 decade ago
Function fgets(char *s,int n ,FILE char *stream)

It stop when it read n-1 char or a newline whichever come first.

Hence in the given question it read only 4 char.

RAHUL said:   1 decade ago
What about fseek guys?

Sintu Kumar Das said:   1 decade ago
In the file the 7th character is 'N' and fgets reads n-1 character.so r is not printed.After 7 character,str gets the null character and stoped. So the remaining 4 will be printed.

Cindrella said:   1 decade ago
Please provide me a detailed description on fseek and fgets.

Preety said:   1 decade ago
What does the 9L means in the code?

Can anybody please tell me abt seek_cur, seek_set, seek_end?

Thilaga said:   1 decade ago
@Sintu: you said remaining 4 char was printed. But there the remaing char is 5.

So 5 has it print but it print 4 char oly .........


Post your comments here:

Your comments will be displayed after verification.