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 3 of 3.

Rohit said:   1 decade ago
fseek takes the current position to N which in the 9th at 9th position, fgets reads the num-1 characters from there on wards.

So the output happens to be agpu.

Milin said:   1 decade ago
Int his program we are move the current pointer by 9 character, so after that SEEK_CUR will points to char 'e' and as we know fgets will read n-1 char from fp and put them into str. That is why we get output as "agpu".

For more learning try this one:

Example:#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;
}

Try to see that what is different in above and this program.

Lohithendra kumar said:   1 decade ago
Thank you guys for saying about fseek.

Sourav Bera said:   1 decade ago
fseek(fp,9L,SEEK_CUR).

Initially the current pointer points to 'T'. After moving 9 character towards right current position, the pointer points to 'a'.

Now fgets (str, 5, fp).

This line will take n-1 characters into the character array as it is given 5. As it stop when it read n-1 char or a newline whichever comes first.
(1)

Chandu said:   9 years ago
How can I get the answer agpur?

Can anyone explain me?

Ojeswani said:   8 years ago
fseek(fp, 9L, SEEK_CUR);
//Here...it goes from 0 to 9 at set cursor at 10th position i.e a.

fgets(str, 5, fp);
//Here in fgets it takes 5 blocks of memory where last block is occupied ny NULL character i.e 0. So it can take upto 4 characters and stores in str.

puts(str);
//prints agpu.
(3)

Dada said:   7 years ago
Agree @Rohit.

Swa said:   7 years ago
Thank you @Ojeswani.

Priya said:   5 years ago
Thank you @Shraddha.

Ushir said:   4 years ago
What is the return type of fseek () ? Please explain.


Post your comments here:

Your comments will be displayed after verification.