Confusing Output From C Program

Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
I have a C program that compiles to an executable called myprogram. This is its main function:
int main(int argc, char ** argv)
printf("this is a test message.n");
system("ls");
return 0;
When I run myprogram > output.txt in a Linux shell and then examine output.txt, I see the output of ls listed above "this is a test message."
I feel like it should be the other way around. Why is this happening, and what can I do so that "this is a test message" appears at the top of output.txt?
If it matters, I'm new to both C and working in a command line.
c linux io-redirection
add a comment |Â
up vote
6
down vote
favorite
I have a C program that compiles to an executable called myprogram. This is its main function:
int main(int argc, char ** argv)
printf("this is a test message.n");
system("ls");
return 0;
When I run myprogram > output.txt in a Linux shell and then examine output.txt, I see the output of ls listed above "this is a test message."
I feel like it should be the other way around. Why is this happening, and what can I do so that "this is a test message" appears at the top of output.txt?
If it matters, I'm new to both C and working in a command line.
c linux io-redirection
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I have a C program that compiles to an executable called myprogram. This is its main function:
int main(int argc, char ** argv)
printf("this is a test message.n");
system("ls");
return 0;
When I run myprogram > output.txt in a Linux shell and then examine output.txt, I see the output of ls listed above "this is a test message."
I feel like it should be the other way around. Why is this happening, and what can I do so that "this is a test message" appears at the top of output.txt?
If it matters, I'm new to both C and working in a command line.
c linux io-redirection
I have a C program that compiles to an executable called myprogram. This is its main function:
int main(int argc, char ** argv)
printf("this is a test message.n");
system("ls");
return 0;
When I run myprogram > output.txt in a Linux shell and then examine output.txt, I see the output of ls listed above "this is a test message."
I feel like it should be the other way around. Why is this happening, and what can I do so that "this is a test message" appears at the top of output.txt?
If it matters, I'm new to both C and working in a command line.
c linux io-redirection
c linux io-redirection
asked 3 hours ago
Archr
1575
1575
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
9
down vote
accepted
By default output to stdout is line-buffered. That is, the buffer is flushed when it's full or when you add a newline.
However that's the default, when stdout is connected to a terminal. If stdout is not connected to a terminal, like what happens when you redirect the output from your program, then stdout becomes fully buffered. That means the buffer will be flushed and actually written either when it's full or when explicitly flushed (which happens when the program exits).
This means that the output of a separate process started from your code (like what happens when you call system) will most likely be written first, since the buffer of that process will be flushed when that process ends, which is before your own process.
What happens when using redirection (or pipes for that matter):
- Your
printfcall writes to thestdoutbuffer. - The
systemfunction starts a new process, which writes to its own buffer. - When the external process (started by your
systemcall) exits, its buffer is flushed and written. Your own buffer in your own process, isn't touched. - Your own process ends, and your
stdoutbuffer is flushed and written.
To get the output in the "correct" (or at least expected) order, call fflush before calling system, to explicitly flush stdout.
add a comment |Â
up vote
3
down vote
I suspect it's because of the order in which the stdout buffer gets flushed, which is not necessarily deterministic. It's possible that the parent spawns the ls process and doesn't flush its own stdout until after that returns. It may not actually flush stdout until the process exits.
Try adding fflush (stdout) after the printf statement and see if that forces the output to appear first.
add a comment |Â
up vote
3
down vote
It is related to output buffering. I managed to reproduce the same behaviour. Forcing the flush did it for me.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char ** argv)
printf("this is a test message.n");
fflush(stdout);
system("ls");
return 0;
Before adding the fflush:
$ ./main > foo
$ cat foo
main
main.c
this is a test message.
and after:
$ ./main > foo
$ cat foo
this is a test message.
foo
main
main.c
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
9
down vote
accepted
By default output to stdout is line-buffered. That is, the buffer is flushed when it's full or when you add a newline.
However that's the default, when stdout is connected to a terminal. If stdout is not connected to a terminal, like what happens when you redirect the output from your program, then stdout becomes fully buffered. That means the buffer will be flushed and actually written either when it's full or when explicitly flushed (which happens when the program exits).
This means that the output of a separate process started from your code (like what happens when you call system) will most likely be written first, since the buffer of that process will be flushed when that process ends, which is before your own process.
What happens when using redirection (or pipes for that matter):
- Your
printfcall writes to thestdoutbuffer. - The
systemfunction starts a new process, which writes to its own buffer. - When the external process (started by your
systemcall) exits, its buffer is flushed and written. Your own buffer in your own process, isn't touched. - Your own process ends, and your
stdoutbuffer is flushed and written.
To get the output in the "correct" (or at least expected) order, call fflush before calling system, to explicitly flush stdout.
add a comment |Â
up vote
9
down vote
accepted
By default output to stdout is line-buffered. That is, the buffer is flushed when it's full or when you add a newline.
However that's the default, when stdout is connected to a terminal. If stdout is not connected to a terminal, like what happens when you redirect the output from your program, then stdout becomes fully buffered. That means the buffer will be flushed and actually written either when it's full or when explicitly flushed (which happens when the program exits).
This means that the output of a separate process started from your code (like what happens when you call system) will most likely be written first, since the buffer of that process will be flushed when that process ends, which is before your own process.
What happens when using redirection (or pipes for that matter):
- Your
printfcall writes to thestdoutbuffer. - The
systemfunction starts a new process, which writes to its own buffer. - When the external process (started by your
systemcall) exits, its buffer is flushed and written. Your own buffer in your own process, isn't touched. - Your own process ends, and your
stdoutbuffer is flushed and written.
To get the output in the "correct" (or at least expected) order, call fflush before calling system, to explicitly flush stdout.
add a comment |Â
up vote
9
down vote
accepted
up vote
9
down vote
accepted
By default output to stdout is line-buffered. That is, the buffer is flushed when it's full or when you add a newline.
However that's the default, when stdout is connected to a terminal. If stdout is not connected to a terminal, like what happens when you redirect the output from your program, then stdout becomes fully buffered. That means the buffer will be flushed and actually written either when it's full or when explicitly flushed (which happens when the program exits).
This means that the output of a separate process started from your code (like what happens when you call system) will most likely be written first, since the buffer of that process will be flushed when that process ends, which is before your own process.
What happens when using redirection (or pipes for that matter):
- Your
printfcall writes to thestdoutbuffer. - The
systemfunction starts a new process, which writes to its own buffer. - When the external process (started by your
systemcall) exits, its buffer is flushed and written. Your own buffer in your own process, isn't touched. - Your own process ends, and your
stdoutbuffer is flushed and written.
To get the output in the "correct" (or at least expected) order, call fflush before calling system, to explicitly flush stdout.
By default output to stdout is line-buffered. That is, the buffer is flushed when it's full or when you add a newline.
However that's the default, when stdout is connected to a terminal. If stdout is not connected to a terminal, like what happens when you redirect the output from your program, then stdout becomes fully buffered. That means the buffer will be flushed and actually written either when it's full or when explicitly flushed (which happens when the program exits).
This means that the output of a separate process started from your code (like what happens when you call system) will most likely be written first, since the buffer of that process will be flushed when that process ends, which is before your own process.
What happens when using redirection (or pipes for that matter):
- Your
printfcall writes to thestdoutbuffer. - The
systemfunction starts a new process, which writes to its own buffer. - When the external process (started by your
systemcall) exits, its buffer is flushed and written. Your own buffer in your own process, isn't touched. - Your own process ends, and your
stdoutbuffer is flushed and written.
To get the output in the "correct" (or at least expected) order, call fflush before calling system, to explicitly flush stdout.
edited 2 hours ago
answered 3 hours ago
Some programmer dude
283k23231387
283k23231387
add a comment |Â
add a comment |Â
up vote
3
down vote
I suspect it's because of the order in which the stdout buffer gets flushed, which is not necessarily deterministic. It's possible that the parent spawns the ls process and doesn't flush its own stdout until after that returns. It may not actually flush stdout until the process exits.
Try adding fflush (stdout) after the printf statement and see if that forces the output to appear first.
add a comment |Â
up vote
3
down vote
I suspect it's because of the order in which the stdout buffer gets flushed, which is not necessarily deterministic. It's possible that the parent spawns the ls process and doesn't flush its own stdout until after that returns. It may not actually flush stdout until the process exits.
Try adding fflush (stdout) after the printf statement and see if that forces the output to appear first.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
I suspect it's because of the order in which the stdout buffer gets flushed, which is not necessarily deterministic. It's possible that the parent spawns the ls process and doesn't flush its own stdout until after that returns. It may not actually flush stdout until the process exits.
Try adding fflush (stdout) after the printf statement and see if that forces the output to appear first.
I suspect it's because of the order in which the stdout buffer gets flushed, which is not necessarily deterministic. It's possible that the parent spawns the ls process and doesn't flush its own stdout until after that returns. It may not actually flush stdout until the process exits.
Try adding fflush (stdout) after the printf statement and see if that forces the output to appear first.
answered 3 hours ago
ConcernedOfTunbridgeWells
50.7k13123185
50.7k13123185
add a comment |Â
add a comment |Â
up vote
3
down vote
It is related to output buffering. I managed to reproduce the same behaviour. Forcing the flush did it for me.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char ** argv)
printf("this is a test message.n");
fflush(stdout);
system("ls");
return 0;
Before adding the fflush:
$ ./main > foo
$ cat foo
main
main.c
this is a test message.
and after:
$ ./main > foo
$ cat foo
this is a test message.
foo
main
main.c
add a comment |Â
up vote
3
down vote
It is related to output buffering. I managed to reproduce the same behaviour. Forcing the flush did it for me.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char ** argv)
printf("this is a test message.n");
fflush(stdout);
system("ls");
return 0;
Before adding the fflush:
$ ./main > foo
$ cat foo
main
main.c
this is a test message.
and after:
$ ./main > foo
$ cat foo
this is a test message.
foo
main
main.c
add a comment |Â
up vote
3
down vote
up vote
3
down vote
It is related to output buffering. I managed to reproduce the same behaviour. Forcing the flush did it for me.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char ** argv)
printf("this is a test message.n");
fflush(stdout);
system("ls");
return 0;
Before adding the fflush:
$ ./main > foo
$ cat foo
main
main.c
this is a test message.
and after:
$ ./main > foo
$ cat foo
this is a test message.
foo
main
main.c
It is related to output buffering. I managed to reproduce the same behaviour. Forcing the flush did it for me.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char ** argv)
printf("this is a test message.n");
fflush(stdout);
system("ls");
return 0;
Before adding the fflush:
$ ./main > foo
$ cat foo
main
main.c
this is a test message.
and after:
$ ./main > foo
$ cat foo
this is a test message.
foo
main
main.c
answered 3 hours ago
Aif
8,74912335
8,74912335
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52534629%2fconfusing-output-from-c-program%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
