C Programming - Input / Output - Discussion

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;
}

[A]. agpur
[B]. gpur
[C]. Nagp
[D]. agpu

Answer: Option D

Explanation:

No answer description available for this question.

Viraj said: (Nov 22, 2010)  
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: (Feb 16, 2011)  
Thanks bro!

Gouthami said: (Feb 25, 2011)  
We want more explanation.

Sai said: (May 26, 2011)  
@viraj.

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

Baadal said: (Jul 5, 2011)  
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: (Aug 6, 2011)  
What about fseek guys?

Sintu Kumar Das said: (Sep 4, 2011)  
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: (Sep 30, 2011)  
Please provide me a detailed description on fseek and fgets.

Preety said: (Nov 16, 2011)  
What does the 9L means in the code?

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

Thilaga said: (Dec 4, 2011)  
@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 .........

Nandu said: (Dec 27, 2011)  
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'

Tnareju said: (Jan 29, 2012)  
seek_cur it sets pointer (fp) currently to 9L (9 th char of that line i.e N)

Yatendra said: (Feb 4, 2012)  
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.

Jogamohan Medak said: (Oct 20, 2012)  
Offset used in fseek() function call can be a negative number.

It makes the file pointer to move backwards from the current position.

Shraddha said: (Nov 6, 2012)  
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'.

AMOL said: (Nov 6, 2013)  
@Preety: 9L means it move forward 9 char before printing.

Aditi said: (Apr 10, 2014)  
If fp points to the 9th character in the string which is N then why fgets is not reading 'N'. Please someone provide me the reason.

Sparsha said: (Oct 4, 2014)  
Why fp is taking a and not N? when 9th letter is N?

Sindhu said: (Mar 2, 2015)  
Please explain these two lines:

fseek(fp, 9L, SEEK_CUR);
fgets(str, 5, fp);

Vishal said: (Mar 3, 2015)  
How agpu is correct please tell?

Rohit said: (Apr 1, 2015)  
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: (Jun 3, 2015)  
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: (Jul 12, 2015)  
Thank you guys for saying about fseek.

Sourav Bera said: (Jul 18, 2015)  
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.

Chandu said: (Feb 10, 2017)  
How can I get the answer agpur?

Can anyone explain me?

Ojeswani said: (Mar 20, 2017)  
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.

Dada said: (Apr 25, 2018)  
Agree @Rohit.

Swa said: (Jan 20, 2019)  
Thank you @Ojeswani.

Priya said: (Dec 6, 2020)  
Thank you @Shraddha.

Ushir said: (Dec 8, 2021)  
What is the return type of fseek () ? Please explain.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.