bdrmachine
Posts: 27
Joined: Fri Sep 20, 2013 8:52 pm

Print buffer

Tue Nov 27, 2018 8:58 pm

My c programs written with netbeans connected to my Pi seems to put all printf output in some kind of buffer before sending to the output window. Is there a way to cause the data to be sent immediately?

User avatar
PeterO
Posts: 4939
Joined: Sun Jul 22, 2012 4:14 pm

Re: Print buffer

Tue Nov 27, 2018 9:05 pm

If your code is the same as in you previous thread, then try adding "\n" (newline) to the end of your strings.
The output is probably line buffered.

Code: Select all

printf("Hello World\n");
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

bdrmachine
Posts: 27
Joined: Fri Sep 20, 2013 8:52 pm

Re: Print buffer

Tue Nov 27, 2018 9:26 pm

Thanks, Excellent suggestion but no change.

Code: Select all

int main (int argc, char** argv)
{
 

 int loopv;  
 
 printf("hello ccc WORld\n");
  
  wiringPiSetup ();
  pinMode (0, OUTPUT) ;
  
  for (loopv=0;loopv<=20;++loopv)

  {
    digitalWrite (0, HIGH) ; delay (800) ;
    digitalWrite (0,  LOW) ; delay (200) ;
    printf("made it this far.");
  }
  
  for (loopv=0;loopv<=35;++loopv)
  {
      printf("done\n");
  }
	  
return 0 ;
}
Here is the output after the program ends. The LED connected to the output pin blinks but no output text until the end of the program.

made it this far.
made it this far.
made it this far.
made it this far.
made it this far.
made it this far.
made it this far.
made it this far.
made it this far.
made it this far.
made it this far.
made it this far.
done
done
done
done
done
done
done
done
done
done
done
done
done
done
done
done
done
done
done
done
done
done
done
done
done
done
done
done
done
done
done
done
done
done
done
done

RUN SUCCESSFUL (total time: 21s)

W. H. Heydt
Posts: 10733
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Print buffer

Tue Nov 27, 2018 9:59 pm

You can "flush" a print buffer to get immediate output. One generally does that when there is an abend condition, but it can be done at any time.

Example:

Code: Select all

  fprintf(log_file,"Enter get att master\n");
  fflush(log_file);

bdrmachine
Posts: 27
Joined: Fri Sep 20, 2013 8:52 pm

Re: Print buffer

Tue Nov 27, 2018 10:04 pm

Great but how?

User avatar
buja
Posts: 498
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: Print buffer

Tue Nov 27, 2018 10:04 pm

That would be:

Code: Select all

fflush(stdout);
right after the printf-statement.

bdrmachine
Posts: 27
Joined: Fri Sep 20, 2013 8:52 pm

Re: Print buffer

Tue Nov 27, 2018 10:06 pm

Thanks Guys that work!

Return to “General discussion”