简体   繁体   中英

Different output printing stdout on console/redirecting to a file

I have a problem with stdout on bash. First, I have a program named Channel_Flow which is written by C++ language. This program basically compute some maths on computational fluid dynamics field.

My problem occur when I use this:
$ ./Channel_Flow
for my program.

The output is different when I redirect the output, like:
$ ./Channel_Flow > result.dat

The difference is really significant, some maths is computed and behave differently. I never have this kind of error before.
Is there anything wrong with the way I execute the program? Anyone has a hint for this?

Thank you. If I need to add some details, I'll gladly edit the question.
(For additional information, I use makefile to compile the file with --c++11 flags and -O2 optimization flags with g++ 5.3.1 compiler)

edit I add some of the outputs from the latter command:
substep - 1 1 1 10 : -18693.7 2 1 10 : -18693.7 3 1 10 : -18693.7 4 1 10 : -18693.7 5 1 10 : -18693.7 6 1 10 : -18693.7 7 1 10 : -18693.7 8 1 10 : -18693.7 9 1 10 : -18693.7

From the first commmand, I got this:

substep - 1 1 1 10 : 3.47858 2 1 10 : 3.47858 3 1 10 : 3.47858 4 1 10 : 3.47858 5 1 10 : 3.47858 6 1 10 : 3.47858 7 1 10 : 3.47858 8 1 10 : 3.47858 9 1 10 : 3.47858

The code is complex so I'm still searching where the source of the problem is. What I'm asking here why my output has a different result when I write it to a file?
The correct one is the console output.

I tried this: (As the first answer suggests).

# have a specific error file
./Channel_Flow > result.dat 2>errors.log
#or all to the same:
./Channel_Flow > result.dat 2>&1

but the file errors.log is empty. The second command gave the exact same result.
EDIT AGAIN

I use an offstream to print my result,

std::ofstream testing("testing.dat");
if (k==0)  {
  testing  <<  i << " " << j << " " << k << " : " << ux[i][j][k] << std::endl;  
  std::cout  <<  i << " " << j << " " << k << " : " << ux[i][j][k] << std::endl;  
}

I use the first command:

$ ./Channel_Flow

Output:
substep - 1 1 1 10 : -18693.7 2 1 10 : -18693.7 3 1 10 : -18693.7 4 1 10 : -18693.7 5 1 10 : -18693.7 6 1 10 : -18693.7 7 1 10 : -18693.7 8 1 10 : -18693.7 9 1 10 : -18693.7

(On both ofstream file (testing.dat) and the console interface)
I use the second command:

$ ./Channel_Flow > result.dat

Output:
substep - 1 1 1 10 : 3.47858 2 1 10 : 3.47858 3 1 10 : 3.47858 4 1 10 : 3.47858 5 1 10 : 3.47858 6 1 10 : 3.47858 7 1 10 : 3.47858 8 1 10 : 3.47858 9 1 10 : 3.47858

(On both ofstream file (testing.dat) and the console interface)

This is probably because you have things going to stdout and other going to stderr , and you redirect both differently.

  • - ie Standard output are normal things coming from the program.

  • - ie Error output, I let you guess.

So in your case, with ./Channel_Flow > result.dat you only redirect stdout to result.dat , leaving stderr display in the console.

If you want every thing to go to files:

# have a specific error file
./Channel_Flow > result.dat 2>errors.log
#or all to the same:
./Channel_Flow > result.dat 2>&1

edit

See what happens in your case with

## and this latest would both log to console and write to file:
./Channel_Flow |tee result.dat 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM