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 1 of 3.
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.
//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)
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.
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)
AMOL said:
1 decade ago
@Preety: 9L means it move forward 9 char before printing.
Ushir said:
4 years ago
What is the return type of fseek () ? Please explain.
Priya said:
5 years ago
Thank you @Shraddha.
Swa said:
7 years ago
Thank you @Ojeswani.
Dada said:
7 years ago
Agree @Rohit.
Chandu said:
9 years ago
How can I get the answer agpur?
Can anyone explain me?
Can anyone explain me?
Lohithendra kumar said:
1 decade ago
Thank you guys for saying about fseek.
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.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers