Skip to main content
GameDev.net gamedev.net
🔒 Locked

using '\r'

Started by Endar Aug 4, 2006 at 7:54 PM 7 replies 1.2k views
Original Post
Endar
Endar
I'm attempting to write a console function (in linux, though I doubt it'll matter), and I'm attempting to use the carriage return so I can have a prompt that prints out the percentage done of the processing I'm doing. But I can't remember how to properly use the carriage return so when I print next, it replaces the last statement that gives the percentage. Here's what I have:

printf("\rAmount of image processed: %d%%", (int)(temp*100) );
I'm currently running linux through putty by logging onto my uni, although I'll mostly be working on this at uni where I'll be running linux on the comp and not through ssh.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Oluseyi
Oluseyi
In most consoles, just use the newline, \n. Carriage return is relevant when sending output to printers or dealing with Mac/Windows "rich text" environments.
Scet
Scet
He doesn't want a newline, he wants a carriage return to overwrite the last line of text.

Anyway Endar have you actually tried anything and if so, what was the result? I made a little test program(Windows, GCC) and it didn't seem to matter if the carriage return was at the beginning or end of the ouput string.

int main(){		for( int i = 0; i <= 100; i++ )	{		cout << "\rAmount of image processed: " << i;		Sleep( 50 );	}	cout << "\n";	for( int i = 0; i <= 100; i++ )	{		cout << "Amount of image processed: " << i << "\r";		Sleep( 50 );	}	cout << "\n";	system( "PAUSE" );	return 0;}
Oluseyi
Oluseyi
Quote:
Original post by Scet
He doesn't want a newline, he wants a carriage return to overwrite the last line of text.

Sorry, that's not going to happen with C++ standard I/O streams. Look into platform-specific APIs or curses libraries.
Kalazart
Kalazart
'\r' will take the cursor to the start of the line, on Windows, at least. So, placing before or after the string won't make difference. If you place after the string, you should assure there is a new line before you write anything else, so you won't overwrite that line. You may want to check '\b', so you can do stuff like this:

printf("Progress: ");while(it_should_loop()) {    printf("%3d%%\b\b\b\b");  \\ writes percentage and backs off 4 chars (3 from number plus percent char)}


But this way looks way uglier than using '\r'...

Note: all this works on Windows (used mingw to compile)... dunno about Linux.
markr
markr
You may find that either stdio and/or the Linux tty layer buffers some of the output until it sees a newline (\n not \r).

This may cause this approach not to work. Consider using the relevant (e.g. fflush, or some sttys) functions to prevent this if it happens.

Mark
Endar
Endar
Aha!!! It didn't work because, well, mostly I'm an idiot. :D

To test it, I chose to put a 'getchar' call after every time it printed to make sure that it would work. Apparently this cause it to go to another line, so instead of getting a single line I would get many. Exactly the same as if the '\r' was replaced with a newline.

This also happened with Scet's demo. So, I found the problem, and it should all work fine now. Thanks guys.

Edit:: Yep, works fine now. I just didn't expect the call to 'getchar' to actually output the character to the screen.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Conner McCloud
Conner McCloud
Quote:
Original post by Anonymous Poster
And how do you think the curses libraries work?

Depends on the environment. The exact behavior of the output streams are fairly unspecified.

CM

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.