Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-11-2006, 10:37 AM   #1
LQ Newbie
Registered: Sep 2006
Posts: 4

Rep: Reputation: 0
workaround for C++ printf carriage return

I am new to Linux and am porting a C++ program from ROM-DOS to Linux. I have managed to get everything working except the screen display (text screen only).
The programme is reading data from an external input and I want to update the screen with the latest readings. I do not want the screen to scroll (i.e. linefeed). I want to keep updating the values on the same line. In the ROM-DOS version I was able to do this by using the \r (carriage return) escape sequence. In the Linux program I don't get anything displayed when I use \r. The display appears when I change to using \n (newline) but then a new line of text is generated for every printf call which is not what I want.
Is there another way of doing this?
Old 09-11-2006, 11:07 AM   #2
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
I think that the problem with \r is that, by default, the stdout is not flushed until a \n is found. So, what you can do is flush the stdout manually everytime you want to update the screen so it gets display.
This is a simple example that worked on my PC:
#include <stdio.h>
#include <time.h>

int main() {
  int n;
  for(n = 0; n < 100; n++) {
    printf("\r%3d", n);
    fflush(stdout);  // This line flushes stdout inmediatly
of course you dont need to sleep(1), thats just for demonstration.

If you want to make more advanced text interface, then you should turn into the ncurses library which lets you do fancy things in text mode.

Hope this is useful.
Old 09-11-2006, 12:13 PM   #3
LQ Newbie
Registered: Sep 2006
Posts: 4

Original Poster
Rep: Reputation: 0
Excellent! That is exactly what I was looking for. Thanks very much.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
grep detecting carriage return, how ? Grafbak Programming 22 01-13-2010 01:19 PM
carriage return in emac lisp balloon Programming 1 11-23-2004 08:29 AM
Inserting a carriage return in awk legtester Linux - General 1 08-17-2003 05:29 PM
unescaping a carriage return in perl acid_kewpie Programming 10 09-20-2002 02:20 PM
pppd...carriage return?!? icyfire Linux - Software 1 02-14-2002 07:07 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:53 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration