Print, delay and erase current line in C - c

I want to print the seconds elapsed in real time since the program began. First the output is "0". After one second, the "0" is replaced by "1", and so on. Here is the code I wrote initially.
#include<stdio.h>
#include<time.h>
void main ()
{
long int time;
printf("Hello, let us measure the time!\n");
time=clock();
printf("%ld", 0);
while(time/CLOCKS_PER_SEC<7)
{
time=clock();
if(time%CLOCKS_PER_SEC==0)
{
printf("\r");
printf("%ld", time/CLOCKS_PER_SEC);
}
}
}
This gave an output "7" at the end of 7 seconds only.
If I make the following replacements, the code works fine.
printf("%ld", 0);
by
printf("%ld\n", 0);
and
printf("\r");
printf("%ld", time/CLOCKS_PER_SEC);
by
printf("\33[A"); //vt100 char, moves cursor up
printf("\33[2K"); //vt100 char, erases current line
printf("%ld\n", time/CLOCKS_PER_SEC);
The problem seems that the output wont be sent until the current line is completely determined. What is happening here?
I am using gcc compiler on Ubuntu.

The output stream that you are writing to with printf() will buffer until the newline is received. Since you aren't sending a newline then you get no flush until your application exits or the buffer fills up.
You can flush the output buffer yourself after each printf(), as hyde said in the comment above using fflush(stdout).
Or you can disable buffering by using setbuf( stdout, NULL );

Related

Print number for each second

#include <stdio.h>
#include <wiringPi.h>
#include <softPwm.h>
int main(void)
{
wiringPiSetupGpio();
for(int i = 0 ; i <50; i++)
{
if ( i%10 == 1)
{
printf("\n");
}
printf("%d ", i);
delay(1000);
}
return 0;
}
I'm working in rasberry pi environment.
I want to print a number for each 1 second. But this code did not print a number one by one but
print 10 numbers for each 10 seconds. This code gives 10 numbers in line at once. What's the problem??
The stdout channel is line buffered by default. This means that data sent to stdout won't necessarily appear until a newline character is printed.
If you call fflush(stdout), any buffered output will be immediately printed.
printf("%d ", i);
fflush(stdout);
in order to print the values at the running time using printf you need to add \n so try this may work.
#include <stdio.h>
#include <wiringPi.h>
#include <softPwm.h>
int main(void)
{
wiringPiSetupGpio();
for(int i = 0 ; i <50; i++)
{
if ( i%10 == 1)
{
printf("\n");
}
// a `\n` added at the end of the string!
printf("%d \n", i);
delay(1000);
}
return 0;
}
for more info read this question's answer
The problem is probably that the output stream is not being flushed. I suggest that you call fflush( stdout ); before the delay(1000); function call. This will ensure that all printed data actually becomes observable, before the program enters a wait state.
Normally, it is not necessary to explicitly flush an output stream, because the output will become visible sooner or later. For example, the output buffer will usually get implicitly flushed whenever you read input or when the program ends. Also, if your program is writing output to the user's screen (in contrast to, for example, writing output to a file), then the output stream is probably line-buffered, which means that the output stream will get implicitly flushed whenever a newline character is written.
However, in this case, it appears that the implicit flushing mentioned above is not sufficient. Therefore, you will have to revert to explicit flushing using the function fflush, as mentioned above.

I use sleep function in ubuntu ,but printf function run very slowly in while loop. why?

I use ubuntu 14.04 version system for below code.
I use below code : (below code is infinite Loop)
#include <stdio.h>
#include <unistd.h>
int flag=0;
int main(void){
printf("program start.\n");
printf("PID=%d\n",getpid());
printf("flag=%d\n",flag);
//-----------I feel weired below do...while... sentences----------//
do{
printf("loop_");
sleep(1);
}while(flag==0);
printf("program exit.\n");
return 0;
}
in the beginning below print result :
root#ubuntu:~/Desktop/my_test_code# ./issue
program start.
PID=3113
flag=0
...........//start waiting here,and don't print "loop_"
then after I waited for long time this program prints a lot of "loop_".
I find it very strange, and should print a string "loop_" and then, wait a second, then print a "loop_" again, and so on, why me waiting for a long time, do it start to print a lot of "loop_" ?
anybody has any idea for my issue . thank you in advance.
printf bufferizes its output (until a given size or \n), so you don't see the output until it flushes it.
changing your loop to
printf("loop_");
fflush(stdout);
sleep(1);
should solve your issue.
Root cause:
Usually stdout's buffer are getting flushed by \n so here loop goes on and stdout did not get chance to buffered its data properly.
Solution:
There are three way here
1) Use \n at the end of printf
printf("loop_\n");
2) call fflush(stdout): after printf in loop.
3) Disabled the buffering for stdout stream using below call in your program
setvbuf(stdout, NULL, _IONBF, 0);

Raspberry Pi: printf() doesn't work with wiringPi

I'm trying a simple code that using wiringPi as here:
#include<wiringPi.h>
#include<stdio.h>
int main(void){
int i;
wirintPiSetup();
pinMode(0,OUTPUT); //a single LED
pinMode(8,INPUT); //tactile switch
for(;;){
delay(500);
//push tactile switch and LED is turning on
if(digitalRead(8)) digitalWrite(0,0);
else digitalWrite(0,1);
printf("%d",digitalRead(8));
}
}
I expected a result of printf() is output to console,
but it doesn't work.
printf() couldn't run in same time with wiringPi APIs?
no warnings at compile. and CPU consumption is always under 4%.
running on Raspbian.
Thanks for your time!
stdout by default is typically line-buffered, meaning it tries to put off writing data to the underlying file until a newline. But since you never print a newline, stdout will just buffer your text until it runs out of space.
You can fix this by either adding a newline in the format string (i.e. "%d\n"), or calling fflush on stdout after printing.

How to pause between functions in a program in C with GCC?

So my problem is my program runs too fast that I can't see how it behaves. It's supposed to make the text crawl along the edges of the terminal. I tried to use sleep() to make a short pause betweenprintf(...)s so that it I can see where the text us going while it prints.
This is what it looks like:
http://i.imgur.com/B6FFbNp.gif
So I put sleep() function after the printfs so that it will pause before starting the loop again and make the text move slowly. But what happens is it pauses the programs indefinitely before it even starts. This also happens with usleep and system("pause 1"). This is what it looks like:
http://i.imgur.com/krGW3lB.gif
==================================================================================
EDIT:
Okay, I figured it out on my own. It seems that sleep() only works if I put \n in my strings. I don't know why. I didn't even read this in the damn manuals.
So if you have
printf("HELLO\n");
sleep(3);
printf("HELLO\n");
sleep(3);
printf("HELLO\n");
It will result in:
HELLO
[pause for 3 seconds]
HELLO
[pause for 3 seconds]
HELLO
but if you remove the newline characters it will:
[pause for 9 seconds]
HELLO HELLO HELLO
I don't know why this happens but it just does
==================================================================================
EDIT:
This is how I wanted my program to work:
http://i.imgur.com/DXv7E60.gif
Thank you for your answers
Your observations do not due to sleep() not working, but to the fact that printf() uses stdout, which is line buffered. "line buffered" means, that what has been written to stdoutis buffered until the end of the line is reached before the buffer's content it is flushed out.
So after the first printf("HELLO"); the output does not go to the screen but stays in the buffer, then sleep(1); is executed and makes you wait. Then for the next printf("HELLO"); the output still does not go to the screen, but the following sleep(1); again makes you wait for 1 second ... and so on until a line end if reached by your program printing a '\n' or by the program's end, which implicitly flushes the output buffer to the console.
You can avoid the behaviour by flushing stdout's output buffer explicitly after every printf():
#include <stdio.h>
#include <unistd.h>
int main(void)
{
printf("HELLO");
fflush(stdout);
sleep(3);
printf("HELLO");
fflush(stdout);
sleep(3);
printf("HELLO");
/* fflush(stdout); */ /* not needed as buffers are implicitly flushed on the program's end */
return 0;
}
A quick hack I usually do is make some buffer array (eg. char buf[10]) and place an fgets() in between iterations, which forces the program to wait for a newline from the user. So, for example, if we had:
.
.
.
for(i = 0; i < 1000000; ++i)
printf("%d\n", i);
we could then do
.
.
.
char buf[10];
for(i = 0; i < 1000000; ++i){
printf("%d\n", i);
fgets(buf, 10, stdin);
}
and control the iterations with the enter key.
We could also stop every nth iteration by using a modulus. Using the above example, we will now stop every 100 iterations:
.
.
.
char buf[10];
for(i = 0; i < 1000000; ++i){
printf("%d\n", i);
if(i % 100 == 0)
fgets(buf, 10, stdin);
}
A more time consuming but effecient way is to use a dedicated debugger like GDB.

Why does printf() not print anything before sleep()?

I'm just learning C with Kernighan and Ritchie's book; I'm in the basics of the fourth chapter ("Functions and Program Structure"). The other day I became curious about the sleep() function, so tried to use it like this:
#include <stdio.h>
#include <unistd.h>
int main(void)
{
printf(" I like cows.");
sleep(5);
return 0;
}
The problem is the output of the program, it looks like it does the sleep() first and then the printf(), in other words, it waits five seconds and then prints the string. So I thought, maybe the program gets to sleep() so fast that it doesn't let printf() have his work done like I want, that is print the string and then sleep.
How can I show the string and then put the program to sleep?
The compiler is GCC 3.3.5 (propolice) in OpenBSD 4.3.
printf() writes to stdout (the default output stream) which is usually line buffered. The buffer isn't flushed by the time sleep is called so nothing is displayed, when the program exits all streams are automatically flushed which is why it prints right before exiting. Printing a newline will usually cause the stream to be flushed, alternatively you could use the fflush function:
int main(void)
{
printf(" I like cows.\n");
sleep(5);
return 0;
}
or:
int main(void)
{
printf(" I like cows.");
fflush(stdout);
sleep(5);
return 0;
}
If you are printing to a stream that is not line buffered, as may be the case if stdout is redirected or you are writing to a file, simply printing a newline probably won't work. In such cases you should use fflush if you want the data written immediately.
Your problem is that printf (and anything else that uses the stdio library to write to stdout (standard output)) is buffered - line buffered if it goes to the console, and size buffered if it goes to a file. If you do a fflush(stdout); after the printf, it will do what you want. You could try just adding a newline ('\n') to your string, and that would do the right thing as long as you don't redirect standard output to a file.
I'm not 100% sure, but I think stderr isn't buffered, which can cause confusion because you might see output you made to stderr before output you previously made to stdout.
Buffering means that all the output is stored in a place (called buffer) and is output after a certain amount of data is present in it. This is done for efficiency reasons.
Some (most?) implementations clear the buffer after a newline when writing to the console, so you can also try
printf(" I like cows.\n");
instead of the call to fflush()
I implemented time encounter as following;
for (int i = 1; i <= 60; i++) {
printf("%02d", i);
fflush(stdout);
sleep(1);
printf("\b\b");
}

Resources