为什么我的输出转到 cout 而不是文件?
我正在一个带有队列的系统上做一些科学工作。当提交到队列时,cout 将输出输出到一个日志文件,该日志文件的名称是通过命令行选项指定的。然而,我也想要一个单独的输出到一个文件,我是这样实现的:
ofstream vout("potential.txt"); ...
vout<<printf("%.3f %.5f\n",Rf*BohrToA,eval(0)*hatocm);
然而它与 cout 的输出混合在一起,我只在我的 Potential.txt 中得到一些神秘的重复数字。这是缓冲问题吗?输出到其他文件的其他实例可以工作...也许我应该将这个实例移离 cout 较多的区域?
I am doing some scientific work on a system with a queue. The cout gets output to a log file with name specified with command line options when submitting to the queue. However, I also want a separate output to a file, which I implement like this:
ofstream vout("potential.txt"); ...
vout<<printf("%.3f %.5f\n",Rf*BohrToA,eval(0)*hatocm);
However it gets mixed in with the output going to cout and I only get some cryptic repeating numbers in my potential.txt. Is this a buffer problem? Other instances of outputting to other files work... maybe I should move this one away from an area that is cout heavy?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在 vout 中发送 printf 返回的值,而不是字符串。
你应该简单地做:
You are sending the value returned by printf in vout, not the string.
You should simply do:
您正在将 C 和 C++ 混合在一起。
printf 是 c 库中的一个函数,它将格式化字符串打印到标准输出。
ofstream
及其<<
运算符是您以 C++ 风格打印到文件的方式。这里有两个选择,可以用 C 方式或 C++ 方式打印出来。
C 风格:
C++ 风格:
You are getting your C and C++ mixed together.
printf
is a function from the c library which prints a formatted string to standard output.ofstream
and its<<
operator are how you print to a file in C++ style.You have two options here, you can either print it out the C way or the C++ way.
C style:
C++ style:
如果这是在 *nix 系统上,您只需编写程序将其输出发送到 stdout,然后使用管道和 tee 命令将输出定向到一个或多个文件。例如
将导致命令的输出被写入输出文件以及控制台。
如果安装了适当的工具(例如 GnuWin32),您也可以在 Windows 上执行此操作。
If this is on a *nix system, you can simply write your program to send its output to stdout and then use a pipe and the tee command to direct the output to one or more files as well. e.g.
will cause the output of command to be written to outfile as well as the console.
You can also do this on Windows if you have the appropriate tools installed (such as GnuWin32).