0

When I run the code below, it runs the system command on the 4th last line before displaying the string 'proceeding' before it! I'm wondering why and how to fix it, any ideas?

if ((strlen(command)>0) && (command[strlen (command) - 1] == '\n'))      
    command[strlen (command) - 1] = '\0';                               
    printf("proceeding");                  // <-- the string
    strcat(command,contents); 
    strcat(command,subject);
    system(command);                       // <-- offending system command
    sleep (1);
    printf("\n ----- Search complete for: [%s]",command);
    getchar();

There are of course variables such as 'command' and 'subject' which are manipulated and declared outside the code above, so If you need context than I will post the rest of the source code below.

Jens
  • 61,963
  • 14
  • 104
  • 160
user4493605
  • 371
  • 1
  • 15
  • You need to flush the buffer. There are many ways to do it ; Look it up on the internet: "Flushing printf buffer". – A. Abramov Jan 26 '15 at 04:46
  • Use `setvbuf(stdout, (char *)NULL, _IONBF, 0);` before you start doing I/O. Then stdout is unbuffered. See answer below. – Jens Jan 26 '15 at 20:39

4 Answers4

2

Pull the chain and flush:

I.e.

After

printf("proceeding"); 

Put

fflush(stdout);

That will flush the stuff in the buffer (bowl!)

Before the system command is executed.

Ed Heal
  • 55,822
  • 16
  • 77
  • 115
1

Try adding a '\n' to the printf.

It forces flushing the printf buffer. Otherwise, it is not necessary that printf immediately prints the passed params. You can google flushing the buffer latter

fkl
  • 5,068
  • 3
  • 24
  • 66
  • What if a newline is undesirable at that position? – Jens Jan 26 '15 at 09:48
  • If you want something to print at that point, it is most likely not an undesirable case to do a newline. Otherwise, if that is really important, you should either flush every time with fflush (not a very good idea to do it very often) OR rather instead use stderr for printing. At any rate, it's important to know \n has that additional aspect to it. Besides, from a teaching perspective for a new programmer - understanding flushing or each of descriptors (out, error, in) and the concept in general takes some more material to digest. – fkl Jan 26 '15 at 20:34
  • Newlines are undesirable in many places. Think of the twirly sequence \|/- or progress indication with successive dots. Adding newlines is not the solution (it's only a sloppy workaround by redefining the problem). Flushing stdout is the proper solution. This can be done without repeated calls to fflush() when the programmer is informed about the setvbuf() call and its possibilities. – Jens Jan 26 '15 at 20:38
  • Certainly, when they are, you can surely go into enough detailed knowledge of error consoles, buffers and the list goes on. There is always a better - more elegant solution requiring more things to be learned. But for some one most likely being a student having written a printf and wondering few steps latter why it didn't do it, \n should always be the first solution taught. This was my experience in terms of teaching, which is why i wrote the last part about flushing buffer. – fkl Jan 26 '15 at 20:41
  • Besides, It is important to understand this behavior with newline whether in printf, scanf or otherwise. This is a common problem faced often, so it compliments. – fkl Jan 26 '15 at 20:45
0

stdout is line buffered, so it will only display what's in the buffer after it reaches a newline

change printf("proceeding"); to printf("proceeding\n"); or flush stdout with fflush(stdout); will do the work.

Community
  • 1
  • 1
D3Hunter
  • 1,239
  • 10
  • 20
0

The stdio stdout stream is line or fully buffered by default. To set it unbuffered (and to avoid having to write a fflush(stdout) after every output operation), use the ISO C setvbuf function declared in stdio.h:

setvbuf(stdout, (char *)NULL, _IONBF, 0);

once before doing the first I/O.

Jens
  • 61,963
  • 14
  • 104
  • 160