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?
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 4 of 6.
Deepa said:
1 decade ago
Why \n is printed?
Sakshi said:
1 decade ago
Explain this again please.
1. printf("India\rBIX");
Output: BIXia.
1. printf("India\rBIX");
Output: BIXia.
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?
"\r\r\n" is stored and why not?
"\r\n" is returned?
Isvariyaa said:
1 decade ago
Also gets printed in Turbo C compiler.
Sharmi said:
1 decade ago
Thanks sundar. Nice explanation.
Viswanath said:
1 decade ago
Depends on the OS guys. In windows, the line break is \r\n but in linux the line break is \n. So when it reads \r, it automatically understands that it has reached end of line and hence stops further input. GCC must have done the same to allow consistency between various OSes. Not sure though.
Rajeev said:
1 decade ago
Think this might also depend on the OS line separator
*nix \n
Windows \r\n
*nix \n
Windows \r\n
(1)
Avinash said:
1 decade ago
Considering all your explanations about \r.
What will str contain if file contains: "india\rbix"?
What will str contain if file contains: "india\rbix"?
Yami said:
1 decade ago
Sundar : Thanks for the nice explanation.
Surendra said:
1 decade ago
@kamal Dua
You are right dude.... That's why it printed \n instead of "\r\n".
Thnx.........
You are right dude.... That's why it printed \n instead of "\r\n".
Thnx.........
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers