I have a simple program (which uses threads) called ./mpace
that uses printf()
in the thread function. 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.
Thanks a lot, the problem was solved simply by using fflush(stdout). I thought printing in a new line would flush the channel, but as paxdiablo explains in a comment here it doesn't:
Why does printf not flush after the call unless a newline is in the format string?
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. 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.