0

I was doing a simple test. Here is the program test1.cpp

#include <fstream>
#include <iostream>

int main()
{

std::ifstream fin;
fin.open("data.txt");
char tmp[10];

for(int i=0;i<5;i++) {
        fin >> tmp;
        std::cout << "tmp = " << tmp << " ";
}
std::cout << std::endl;
fin.close();
}

and here is the file data.txt.

1234
5678
9abc
39ab
539b

When I run the program, (I compile it with make CPPFLAGS+=-g test1) I get

tmp = 1234 tmp = 5678 tmp = 9abc tmp = 39ab tmp = 539b

I can run it with debugger (I use ddd with gdb inside). The result is the same. But when I single step through the for loop, it doesn't print anything. Why is it like that? ADD : from the comment, I recalled there is this buffering and I can use flush to make it print. But I don't understand why it doesn't get printted with single stepping. Somehow the buffer should fill and should be output to monitor. Why? The 'duplicate question' doesn't give me the answer.

Chan Kim
  • 5,177
  • 12
  • 57
  • 112
  • 4
    The streams are *buffered*. The output will be written when the buffer is full or explicitly flushed. – Some programmer dude Nov 15 '18 at 11:44
  • ok, I remember seeing this buffer thing before. I think printf is also buffered. But with single stepping, why does it disappear? somehow it should flush out as the buffer gets full. But why disappear? The 'duplicate question' doesn't give me the answer. – Chan Kim Nov 15 '18 at 12:02
  • 1
    The buffer is probably larger than the output you write, therefore no single iteration will fill it up and cause it to be flushed. Add a `<< std::flush` at the end of your output line and it should explicitly flush. – Some programmer dude Nov 15 '18 at 12:06
  • Yes, I already did that(I remember using that long time ago also). But why is it not printed when single stepping? when it's printed with just small amount of print when I do it in fast normal run? – Chan Kim Nov 15 '18 at 12:13
  • 1
    Again, the output you write is ***to small*** to fill the entire buffer and cause it to be flushed. Therefore each iteration of the loop just appends to the buffer and that's it. – Some programmer dude Nov 15 '18 at 12:16
  • The operation you are performing while single stepping writes to a buffer and then, if the buffer is full, flushes it. Since the buffer is not full, it does not flush it. That you are single stepping doesn't change the fact that the code you are single stepping through only flushes the buffer if it is full. It is not full, so the code does not flush the buffer. It is not coded to. Until you step into some code that flushes the buffer, the buffer will not be flushed. – David Schwartz Nov 15 '18 at 12:47
  • But why is it flushed when I just run it? And isn't it wrong the data in the buffer is not flushed out and just cleared during single stepping? – Chan Kim Nov 15 '18 at 13:34
  • @Someprogrammerdude I just saw your answer in https://stackoverflow.com/questions/8238461/gdb-printf-strange-output-when-debugging, and I saw that the buffer line is printed when I send std::endl at the final line. Thanks! (but it had been better if you had pointed this out earlier to me.. not that I delved into this. I was working on something else in the mean time.) – Chan Kim Nov 15 '18 at 13:37
  • That's because `std::endl` write a newline *and* then flushes the buffer (adding `<< std::endl` is equivalent to `<< '\n' << std::flush`). And you should [generally prefer plain `'\n'` to `std::endl`](https://stackoverflow.com/questions/213907/c-stdendl-vs-n) (without an explicit flush, as it degrades performance considerably). – Some programmer dude Nov 15 '18 at 13:45
  • Ah, thanks for the info! – Chan Kim Nov 15 '18 at 13:49

0 Answers0