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 2 of 6.
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!
Nischal said:
1 decade ago
@Sundar
I didn't get your example.
\r is to move cursor to left most position in the current line
printf("india\rbix");
Here output must be, first it prints india then \r so again cursor is moved to left most position right?
Please help me this doubt.
I didn't get your example.
\r is to move cursor to left most position in the current line
printf("india\rbix");
Here output must be, first it prints india then \r so again cursor is moved to left most position right?
Please help me this doubt.
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!
Vivek said:
1 decade ago
Please any one clear exlpain.
I don't understand.
I don't understand.
(1)
Nutan said:
1 decade ago
@kamal dua
Thanks for explain it.....
Thanks for explain it.....
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.........
Yami said:
1 decade ago
Sundar : Thanks for the nice explanation.
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"?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers