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.

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.

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)

Shraddha said:   1 decade ago
In function fseek(fp, 9L, SEEK_CUR);the fp points to the 9th character in string which in 'N'..then the function fgets(str, 5, fp);reads in at most one less than size(5) characters from fp and stores them into the buffer pointed to by str. so we now have 'agpur' and fgets will give us 'agpu'.

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)

Nandu said:   1 decade ago
Actualy fseek will move to n bcoz in fseek we written fseek_cursor at 9 bit so it will move to n
now coming to fgetc it will read upto n-1 bit only so it will read upto u(n-1 bit z u,nth bit z r it will read upto n-1bit)so it will start reading at 'a' and end at 'u'

So answer z 'agpu'

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.

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.

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.

Yatendra said:   1 decade ago
fseek() points to 9th char from starting or the 10 th char i.e (point to N or a)?

If it is point to 10th char then agpu is correct otherwise Nagp is correct.

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


Post your comments here:

Your comments will be displayed after verification.