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.

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)

Sundar said:   1 decade ago
@Nayna

Let us know the difference between the \r and \n.

\r - is moves the cursor to the left-most position of the CURRENT line.

\n - is moves the cursor to the left-most position of the NEXT line.

Example:

1. printf("India\rBIX");

Output: BIXia

2. printf("India\nBIX");

Output:

India
BIX

Note: It may not work as I said in 32-bit platform. But works fine in Turbo C (under DOS 16-bit platform).

Hope the understand the difference. Have a nice day!

Sushil said:   1 decade ago
Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or a the End-of-File is reached, whichever comes first.

A newline character makes fgets() stop reading, but it is considered a valid character and therefore it is included in the string copied to str.

A null character is automatically appended in str after the characters read to signal the end of the C string.

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!

Sunil said:   1 decade ago
@Mohit.

You can understand by the example.

printf("indiacon\rbix");

It produce an output as bixiacon.

The main function of \r is ,it prints the string which are written in right side and count the number of character(here bix=3) and then the cursor move to the left and skip as many number of character which was read from the right side of \r(here 3).

Rajnandinee said:   5 years ago
Hello guys!
I hope you are doing well.

1. Well there is fputs() Function in C which reads string in the format of "\r\n"

2.But at the time of reading fgets() function reads only "\n"...Not like previously... "\r\n"

3. And \0 is there just because In C string always ended with null (\0) character.

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.

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.

Chetan said:   1 decade ago
"\r" will be stored in buffer.

If you try to print str (buffer) with printf or in a file you will not able to see the difference because both are white space. Try to print buffer with debugger like GDB or you can print character one by one.

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)


Post your comments here:

Your comments will be displayed after verification.