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;
}
Discussion:
30 comments Page 2 of 3.
Vishal said:
1 decade ago
How agpu is correct please tell?
Sindhu said:
1 decade ago
Please explain these two lines:
fseek(fp, 9L, SEEK_CUR);
fgets(str, 5, fp);
fseek(fp, 9L, SEEK_CUR);
fgets(str, 5, fp);
Sparsha said:
1 decade ago
Why fp is taking a and not N? when 9th letter is N?
Aditi said:
1 decade ago
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.
AMOL said:
1 decade ago
@Preety: 9L means it move forward 9 char before printing.
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'.
Jogamohan Medak said:
1 decade ago
Offset used in fseek() function call can be a negative number.
It makes the file pointer to move backwards from the current position.
It makes the file pointer to move backwards from the current position.
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.
If it is point to 10th char then agpu is correct otherwise Nagp is correct.
Tnareju said:
1 decade ago
seek_cur it sets pointer (fp) currently to 9L (9 th char of that line i.e N)
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'
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'
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers