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.
Nayna said:
2 decades ago
Can anyone explain me...
Why \r doesn't get read..
and if your ans is that because it is a carraige return.
then why \n gets read..it is also a new line type.
Why \r doesn't get read..
and if your ans is that because it is a carraige return.
then why \n gets read..it is also a new line type.
Sairamteja said:
2 decades ago
The question is good and \r is a carriage return.
NISHANTH SHETTY K said:
2 decades ago
Because it is a carraige return. It does not store in str.
Amaresh said:
2 decades ago
Explain in details the answer.
Isvariyaa said:
1 decade ago
Also gets printed in Turbo C compiler.
Nikita said:
1 decade ago
Why in ans there is '\0'.
Jyoti said:
1 decade ago
Nikita, because In C string always ended with null (\0) character.
Kamal dua said:
1 decade ago
This question is on I/O.
In C when we attempt to write a string(which is done using function fputs()).
It converts "\n" into "\r\n".
But while reading using function(which is done using function fgets()). "\r\n" is converted to "\n".
Actually this is a feature of standard library functions.
Hope you guys understand. Have a nice day!
In C when we attempt to write a string(which is done using function fputs()).
It converts "\n" into "\r\n".
But while reading using function(which is done using function fgets()). "\r\n" is converted to "\n".
Actually this is a feature of standard library functions.
Hope you guys understand. Have a nice day!
Nisha said:
1 decade ago
Everyone wrote whatever is already known, but the question is still unanswered. Why the hell \r didn't get stored while \n does!
Anyone. Give the to 'to the point answer' plz!
Anyone. Give the to 'to the point answer' plz!
Nutan said:
1 decade ago
@kamal dua
Thanks for explain it.....
Thanks for explain it.....
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers