重定向不起作用

编程入门 行业动态 更新时间:2024-10-27 00:24:23
本文介绍了重定向不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想将程序的输出放入文件中.我输入了以下内容:

I want to put my program's output into a file. I keyed in the following :

./prog > log 2>&1

但是文件日志" 中没有任何内容.我正在使用Ubuntu 11.10,默认外壳为bash.

But there is nothing in the file "log". I am using the Ubuntu 11.10 and the default shell is bash.

任何人都知道造成这种情况的原因以及如何调试它?

Anybody know the cause of this AND how I can debug this?

推荐答案

有很多可能的原因:

  • 当您尝试通过截断重定向到文件时,程序会从log文件读取输入(请参阅为什么对文件1和文件1进行排序"不起作用?)
  • 对输出进行缓冲,以便在刷新输出缓冲区之前,您不会在文件中看到数据.如果使用C ++ I/O流等,则可以手动调用fflush或输出std::flush.
  • 程序足够聪明,如果输出流不是终端,则禁用输出.
  • 您查看了错误的文件(即在另一个目录中).
  • 您尝试错误地转储文件内容.
  • 您的程序将第一个字符输出为'\ 0',因此即使有一些数据,输出也似乎为空.
  • 命名自己的名字.
  • The program reads the input from log file while you try to redirect into it with truncation (see Why doesn't "sort file1 > file1" work?)
  • The output is buffered so that you don't see data in the file until the output buffer is flushed. You can manually call fflush or output std::flush if using C++ I/O stream etc.
  • The program is smart enough and disables output if the output stream is not a terminal.
  • You look at the wrong file (i.e. in another directory).
  • You try to dump file's contents incorrectly.
  • Your program outputs '\0' as the first character so the output appears to be empty, even though there is some data.
  • Name your own.

您最好的选择是在调试器(如gdb)下运行此应用程序,或使用strace或ptrace(或两者)查看程序在做什么.我的意思是,实际上,输出重定向在过去的40年中一直有效,因此问题一定存在于其他地方.

Your best bet is to run this application under a debugger (like gdb) or use strace or ptrace (or both) and see what the program is doing. I mean, really, output redirection works for the last like 40 years, so the problem must be somewhere else.

更多推荐

重定向不起作用

本文发布于:2023-10-09 10:35:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1475505.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   不起作用

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!