COUT not displaying

I'm writing a small program to mimic the unix tail -f command. I'm confident I am doing it correctly, but for some reason none of the cout's are printing to the console. I through in the cout << "Start" << endl; right at the beginning in case the code was getting stuck somewhere, but it doesn't even display!

I'm using the linux Code::Blocks IDE, console comes up and stays open. Even tried running the compiled binary directly in the terminal, runs but nothing prints.

Thanks in advance for any idea's!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    cout << "Start" << endl;

    string logname = "/var/log/syslog";
    string line;

    ifstream in;
    in.open(logname.c_str());
    in.seekg(0, ios::end);

    streampos start_end = in.tellg();
    streampos new_end = in.tellg();

    if (in.is_open()){
        cout << "Log open" << endl;
    }

    while (true) {
        new_end = in.tellg();

        if (new_end > start_end){
            sleep(2);
        }
        else
            getline(in, line);
            cout << line << endl;
            start_end = in.tellg();
    }
    
    return 0;
}
It could be that the file fails to open. In that case start_end will probably be equal to new_end so the else part of the if statement in the loop will be run. The call to getline will return immediately because the stream is in an error state. The next line will print an empty line (because line is empty). The loop will run very fast and output a lot of empty lines so you never have time to notice any output at the beginning
Last edited on
Even if that is the case the cout << "Start" << endl; at the beginning of main should still print; which it doesn't.
It might output so fast that the Terminal window never actually shows it on the screen. Put a call to sleep right before the loop and see what happens.
Thanks, that was it. It shouldn't be scrolling down though without printing the new lines from syslog. Must be something in my logic, changed the while loop to while (in.is_open()) and it definitely is opening.
Is this the behavior you want ?
http://linux.about.com/library/cmd/blcmdl1_tail.htm

Because in your code, you open the file, move the cursor to the end and attempt to read continously from the file, which obviously does not work.

Re-think your algorithm, because this one is seriously broken.
Yeah, already figured that out. had some inverse logic going on. Thanks anyway!
Topic archived. No new replies allowed.