carriage return and fprintf

R

Rahul

Hi Everyone,

I actually had a program which prints the output to stdout.

while(1)
{
printf("\r sending %d",send_count);
}

Because, of the carriage return escape sequence, the actual output
comes in a single line.
I tried to redirect the output to a file using fprintf(). But for
some reason, the carriage return
doesn't work and the output falls on the next line :-(. I'm not sure
as to why \r doesn't work when
\n works. Is there any work around?

Thanks in advance ! ! !
 
R

Rahul

Use fseek(3) to set the file position indicator.

I thought about seeking the position, but thats a bit tedious as i
have to calculate and book-keep the position in the file.
By the way, \r doesn't work with file streams?
 
J

James Kuyper

Rahul said:
Hi Everyone,

I actually had a program which prints the output to stdout.

while(1)
{
printf("\r sending %d",send_count);
}

Because, of the carriage return escape sequence, the actual output
comes in a single line.
I tried to redirect the output to a file using fprintf(). But for
some reason, the carriage return
doesn't work and the output falls on the next line :-(. I'm not sure
as to why \r doesn't work when
\n works. Is there any work around?

What do you expect '\r' to do? How do you expect it to differ from '\n'?
How do you know that it didn't "work"?
 
R

Rahul

What do you expect '\r' to do? How do you expect it to differ from '\n'?
  How do you know that it didn't "work"?

stdio:

the printf prints the text in the same line (every entry into the
loop)

file:

the printf prints each line into a new line

sending 1
sending 2
sending 3
...
...
...
 
B

Ben Bacarisse

Rahul said:
stdio:

the printf prints the text in the same line (every entry into the
loop)

file:

the printf prints each line into a new line

sending 1
sending 2
sending 3

I think James is trying to get you to think at a deeper level.
I.e. to ask (and answer) questions like "what is a line?".

For me, a line is a possibly empty sequence of characters followed by a
line ending that varies from system to system. If your C library
really is generating multiple lines from your program, then it is
making a mistake. However, no one here can verify that from the out
put you show since it is hand edited. If you included, say, a hex
dump we'd know of there was a big in your C implementation or if you
were just misunderstanding the file's contents.

Even if everything is working correctly, the result is unlikely to be
what you want. It would help if you stated what effect you want at a
higher level. Having output on multiple lines is usually an
advantage, so why do you want all this rather unhelpful output on one
line? Programs that use the \r trick to show progress as they run
usually /don't/ use it when logging data to a file since it is
unhelpful to have all that extra text in a log file.
 
J

John F. Eldredge

Hi Everyone,

I actually had a program which prints the output to stdout.

while(1)
{
printf("\r sending %d",send_count);
}

Because, of the carriage return escape sequence, the actual output
comes in a single line.
I tried to redirect the output to a file using fprintf(). But for
some reason, the carriage return
doesn't work and the output falls on the next line :-(. I'm not sure
as to why \r doesn't work when
\n works. Is there any work around?

Thanks in advance ! ! !

The carriage return \r means return the print position to the start of
the current line. The line feed \n means move down a line. Different
operating systems have different conventions on how lines of text are
terminated. For example, MSDOS and Windows use CRLF (\r\n) at the end of
lines, and Unix or Linux systems use just LF (\n). I have heard of some
operating systems that use just a \r. What results you get when you
print these files to a printer, or view them on screen, depends upon your
equipment and current settings.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top