简体   繁体   English

无法将Linux程序的输出存储到文件

[英]Fail to store output of linux program to a file

I have a simple program (which uses threads) called ./mpace that uses printf() in the thread function. 我有一个名为./mpace的简单程序(使用线程),该程序在线程函数中使用printf() When I ran it from the terminal, the output is printed as it should, but when I type: 当我从终端运行它时,输出将按其应有的方式打印,但是当我键入时:

**./mpace > text**

the file created is empty. 创建的文件为空。 The funny thing is that yesterday it worked perfectly. 有趣的是,昨天它运行良好。 I typed: 我输入:

echo "test" > text

to check if there is a serious problem with this function, but it worked. 检查此功能是否存在严重问题,但是可以正常工作。 So, what could be the reason my program unexpectedly started failing to write to file? 那么,我的程序意外开始无法写入文件的原因可能是什么?

Please note I'd prefer not to use fprintf() from my code, since time consumption is essential. 请注意,我不希望从代码中使用fprintf() ,因为时间消耗至关重要。


Thanks a lot, the problem was solved simply by using fflush(stdout). 非常感谢,使用fflush(stdout)即可解决问题。 I thought printing in a new line would flush the channel, but as paxdiablo explains in a comment here it doesn't: 我以为在新行中打印会刷新通道,但是正如paxdiablo在此处的评论中解释的那样,它不会:

Why does printf not flush after the call unless a newline is in the format string? 除非换行符在格式字符串中,否则为什么在调用后printf不会刷新?

You are right that one should offer as much as information as he can,but I thought that sharing 100 lines of code for a single printf() would be a drag for you to read. 没错,应该提供尽可能多的信息,但是我认为为单个printf()共享100行代码将是您阅读的一个障碍。 I see this was a bad decision, since not using fflush and the fact that the program is being terminated by a signal both contributed to the problem and should have been made known. 我看到这是一个错误的决定,因为不使用fflush,并且程序被信号终止的事实不仅导致了问题,而且应该已经知道。

Could be a buffering issue. 可能是一个缓冲问题。 stdout is line buffered only if it goes to a character device. 仅当stdout转到字符设备时才对其进行行缓冲。 To check if this is the problem, call fflush(stdout) manually after every printf call. 要检查是否是问题所在,请在每次调用printf之后手动调用fflush(stdout) Make sure that the output buffer of stdout is flushed correctly before your program terminates. 在程序终止之前,请确保正确清除了stdout的输出缓冲区。

Well it could be because you are using > instead of >> and probably somehow your code ( which by the way, you didn't share it ) contains a newline (blank) and delete the old one. 可能是因为您使用的是>而不是>>,并且代码可能以某种方式( 顺便说一下,您没有共享 )包含换行符(空白)并删除了旧的换行符。 let me show you an Example. 让我给你看一个例子。

Let say we have the following file called file1.txt which contains the following information: 假设我们有一个名为file1.txt的文件,其中包含以下信息:

 Michael Jimmy Dolores 

And I wont to extract the name Jimmy and redirect the output to another file called file2.txt 而且我不会提取名称Jimmy并将输出重定向到另一个名为file2.txt的文件

cat file1.txt | grep "Jimmy" > file2.txt

Now the file2.txt contains: 现在,file2.txt包含:

Jimmy 吉米

Now lets run this command 3-4 times and check that file again: 现在,让此命令运行3-4次,然后再次检查该文件:

cat file2.txt

Output: 输出:

Jimmy 吉米

Why is this ? 为什么是这样 ? it should be: 它应该是:

 Jimmy Jimmy Jimmy Jimmy 

Well becouse i used > instead of >>. 好吧,因为我用的是>而不是>>。 Now let's try again: 现在让我们再试一次:

michi@michi-laptop:~$ cat file1.txt | grep "Jimmy" >> file2.txt 
michi@michi-laptop:~$ cat file1.txt | grep "Jimmy" >> file2.txt 
michi@michi-laptop:~$ cat file1.txt | grep "Jimmy" >> file2.txt 
michi@michi-laptop:~$ cat file1.txt | grep "Jimmy" >> file2.txt

Output: 输出:

 Jimmy Jimmy Jimmy Jimmy Jimmy 

This could be one of the reasons. 这可能是原因之一。

EDIT: I come with another short example, let us modify Jimmy with Timmy: 编辑:我来了另一个简短的示例,让我们用蒂米修改吉米:

 Michael Timmy Dolores 

Remember the file2.txt looks like this: 记住file2.txt看起来像这样:

 Jimmy Jimmy Jimmy Jimmy Jimmy 

Now if you run: 现在,如果您运行:

cat file1.txt | grep "Jimmy" > file2.txt

I just got myself with an empty file because of > instead of >> , there is no Jimmy in file1.txt . 我只是因为>而不是>>而得到一个空文件, file1.txt中没有Jimmy。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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