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 1 of 6.

Asfahan Ahmad said:   4 years ago
Assume you have a string and you need to override two characters from the beginning.

Then the code like this

Printf(Hyderabad\raa)

Output:
aaderabad
But if you do not put anything after r then;
The given string will be as it is.
(5)

Rahul said:   1 decade ago
What about \r ? Why it is not reading? Can any one explain this please?
(1)

Jayesh rathod said:   3 years ago
Thanks @Kamal.
(1)

Shambavi said:   4 years ago
Thank You @Aravind.
(1)

Aravind said:   5 years ago
@All.

\r, and \n.

while using fputs() function. it actually puts the char,string value in it.

so in this case;

\r moves the cursor to the left side of the present line. and puts the value whatever behind it.
so printf(" indiabix\rbix)
which is bixiabix. which is actually used for puts function type.

so since the string values is stored in an array. and gets() function is used it doesn't change value just to get the value.

and after the end of the string statement in an array. \0 is used just to ensure the statement is over which is actually use as white spaces.
(1)

Tanya said:   8 years ago
Yes @Suman.

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

Suman said:   8 years ago
@Sundar. Please, explain again.

printf("india\rBIX") should print india since you said the control shifts to a leftmost position that is i?
(1)

Gaurav said:   8 years ago
"I am a boy\n\0"

"\n\0" why the two came in the picture and where is gone \r.

please, don't tell me above things again. Explain me in a different way.
(1)

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

Vivek said:   1 decade ago
Please any one clear exlpain.

I don't understand.
(1)


Post your comments here:

Your comments will be displayed after verification.