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.
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.
So the output happens to be agpu.
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.
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
fgets reads n-1 characters or till it encounters \n in input whichever comes firsts
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)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers