C Programming - Input / Output - Discussion

4. 

If the file 'source.txt' contains a line "Be my friend" which of the following will be the output of below program?

#include<stdio.h>

int main()
{
    FILE *fs, *ft;
    char c[10];
    fs = fopen("source.txt", "r");
    c[0] = getc(fs);
    fseek(fs, 0, SEEK_END);
    fseek(fs, -3L, SEEK_CUR);
    fgets(c, 5, fs);
    puts(c);
    return 0;
}

[A]. friend
[B]. frien
[C]. end
[D]. Error in fseek();

Answer: Option C

Explanation:

The file source.txt contains "Be my friend".

fseek(fs, 0, SEEK_END); moves the file pointer to the end of the file.

fseek(fs, -3L, SEEK_CUR); moves the file pointer backward by 3 characters.

fgets(c, 5, fs); read the file from the current position of the file pointer.

Hence, it contains the last 3 characters of "Be my friend".

Therefore, it prints "end".


Cheran said: (Jul 4, 2011)  
Can some explain this very Clear. I am confused how it went to 'end'.

Krishan said: (Nov 3, 2011)  
fseek moves the file pointer to the desired position. the last character in the file being EOF, fseek(fs, 0, SEEK_END) moves the poointer to EOF position. now it moves back to 3 characters again to the left that is to 'e' ....
f r i e n d EOF. so, now it prints from current position i.e. end

Abc said: (Dec 14, 2011)  
What is c[0]=getc(fs); for?

Bhavin said: (May 23, 2012)  
What is the meaning of -3L here?? How it is different from normal -3?

Priya said: (Jun 14, 2012)  
Please clarify the use of c[0] = getc(fs) ;

Would removing it have any affect on the program ?

Siva Prasad said: (Jan 29, 2014)  
@Krishna, You Explained Very Clearly but what use of 5 in statement " fgets(c, 5, fs); " ? If anybody knows the answer?

Tash said: (Mar 13, 2014)  
I think the '5' means... it will return up to 5 characters.!

#include<stdio.h>

int main()
{
FILE *fs, *ft;
char c[10];
fs = fopen("source.txt", "r");
c[0] = getc(fs);
fseek(fs, 0, SEEK_END);
fseek(fs, -5L, SEEK_CUR);
fgets(c, 3, fs);
puts(c);
return 0;
}

/* output */

rie

Agent Dash said: (May 8, 2014)  
What is -5L here?

Advin said: (Aug 16, 2014)  
-5L takes position of cursor back to 5 character or addition to offset address which is end character.

Venkat said: (Sep 30, 2014)  
@Tash in your program -5L means we have to move 5 chars backword, i.e., now our courser is at i;

And in fgets statement you mention 3.
So compiler reads up to 3 chars from current position i, so in this case our answer is ien.

Kacper said: (Oct 13, 2014)  
Segfault for me. C array has no null character, '\0'.

kacper@tower-mint ~/ctests $ cat io04.c
#include<stdio.h>

int main()
{
FILE *fs, *ft;
char c[10];
fs = fopen("source.txt", "r");
c[0] = getc(fs);
fseek(fs, 0, SEEK_END);
fseek(fs, -3L, SEEK_CUR);
fgets(c, 5, fs);
puts(c);
return 0;
}
kacper@tower-mint ~/ctests $ gcc io04.c
kacper@tower-mint ~/ctests $ ./a.out
Segmentation fault
kacper@tower-mint ~/ctests $

Gagandeep said: (Oct 25, 2015)  
c[0] = getc(fs);
puts(c);

What does these two commands do?

Vijay Chaudhari said: (Nov 17, 2015)  
int main()
{
int k=35;
printf("%d %d %d",k==35,k=50,k>40);
}

Can anyone gives me detail about this puzzle?

Answer is 0 50 0 but actual answer given is 0 50 1, How is this possible?

Ch.Suresh Royal said: (Dec 14, 2016)  
According to me, the answer is 0 50 0.

Note : order of evaluation from right to left.

i) k>40->FALSE ->0:
II) K=50 ->here once the K value changed to 50 : it prints K=50.
III) K==35-> Here K is 50 so 50==35-> FALSE->0

FINALLY OUT PUT : 0 50 0

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.