C Programming - Input / Output - Discussion

Discussion Forum : Input / Output - General Questions (Q.No. 1)
1.
In a file contains the line "I am a boy\r\n" then on reading this line into the array str using fgets(). What will str contain?
"I am a boy\r\n\0"
"I am a boy\r\0"
"I am a boy\n\0"
"I am a boy"
Answer: Option
Explanation:

Declaration: char *fgets(char *s, int n, FILE *stream);

fgets reads characters from stream into the string s. It stops when it reads either n - 1 characters or a newline character, whichever comes first.

Therefore, the string str contain "I am a boy\n\0"

Discussion:
57 comments Page 3 of 6.

Kalyani said:   1 decade ago
@Richard.

\0 is a default null character that will be stored automatically at the end of the file when we use file related functions in C.

Gyani said:   1 decade ago
And the question still remains the same. Why not ?

"\r\r\n" is stored and why not?

"\r\n" is returned?

Anis gupta said:   1 decade ago
Hi dear.

I think the answer will be D, because newline return require.

For Linux \n.
For windows \r\n.
For mac os \r.

Avinash said:   1 decade ago
Considering all your explanations about \r.
What will str contain if file contains: "india\rbix"?

Surendra said:   1 decade ago
@kamal Dua
You are right dude.... That's why it printed \n instead of "\r\n".
Thnx.........

Tanya said:   8 years ago
Yes @Suman.

But that overwrites 'ind' and changes it to 'bix' and hence the output.
(1)

Vishnu said:   1 decade ago
Explain this again please.

1. printf("India\rBIX");

Output: BIXia.

Sakshi said:   1 decade ago
Explain this again please.

1. printf("India\rBIX");

Output: BIXia.

Priya said:   7 years ago
printf("india\rBIX") I am not understanding this. Please explain me.

Rajeev said:   1 decade ago
Think this might also depend on the OS line separator
*nix \n
Windows \r\n
(1)


Post your comments here:

Your comments will be displayed after verification.