ifstream not working in Qt 5.8 with clang on Mac



  • I'm trying to open a simple text file and print it. I'm writing code in Qt 5.8 with clang compiler on Mac OS Sierra but after running there is nothing printed in console. this is my code

    #include <QCoreApplication>
    #include <iostream>
    #include <fstream>
    #include <sstream>
    
    using namespace std;
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        ifstream myfile("/Users/amir_p/Desktop/Untitled.txt",fstream::binary);
        string line;
        cout<<myfile.is_open();    
        getline(myfile,line);
        myfile.close();
        cout<<line;
        return a.exec();
    }
    

    or

    #include <QCoreApplication>
    #include <iostream>
    #include <fstream>
    #include <sstream>
    
    using namespace std;
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        ifstream myfile;
        myfile.open("/Users/amir_p/Desktop/Untitled.txt");
        string line;
        cout<<myfile.is_open();    
        getline(myfile,line);
        myfile.close();
        cout<<line;
        return a.exec();
    }
    

  • Moderators

    @Amir_P Being that you are using Qt why would you not use QFile for this?

    As for your problem:

    1. You open an obvious text file as binary. You want to do that as text or problems can occur.
    2. You never test whether the file is opened or not. You could be getting an access denied, file doesn't exist, etc. You need to check if it is open before reading from it. Unless you are absolutely sure you read the result of cout << myfile.is_open() properly every time.
    3. Since you are reading in binary it is possible that you are receiving a null or 0 which causes line to be an empty string.

    Here is the results on my test using clang on the newest xcode:

    [vortex] ~ > clang++ main.cpp
    [vortex] ~ > ./a.out
    line was: #include <iostream>
    [vortex] ~ > clang --version
    Apple LLVM version 8.0.0 (clang-800.0.42.1)
    Target: x86_64-apple-darwin16.0.0
    Thread model: posix
    InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
    

    As you can see works for me.

    Here is the main.cpp code:

    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
            ifstream f("main.cpp");
            if (!f.good())
                    cout << "file could not be opened" << endl;
    
            string line;
            getline(f, line);
            cout << "line was: " << line << endl;
            return 0;
    }
    

    I really think you should move to QFile though if you are going to use Qt. It's kind of the point of using Qt over pure c++. ;)



  • @ambershark I'm trying to learn c++ at first so I don't want to use Qt frameworks. you told I'm reading file as a binary but in the second code I posted I'm not doing that but I'm getting the same result. the file is exist and I opened it using terminal with the same directory and everything was just fine. In your code you're addressing your file as "main.cpp". how can this be opened? it's just files name not a directory.
    thanks


  • Moderators

    @Amir_P said in ifstream not working in Qt 5.8 with clang on Mac:

    @ambershark I'm trying to learn c++ at first so I don't want to use Qt frameworks. you told I'm reading file as a binary but in the second code I posted I'm not doing that but I'm getting the same result. the file is exist and I opened it using terminal with the same directory and everything was just fine. In your code you're addressing your file as "main.cpp". how can this be opened? it's just files name not a directory.
    thanks

    It is because main.cpp was in the same directory. It's called relative path. So it will use the current working directory if you don't specify a path.

    What you are using is an absolute path.

    Try putting my code into a file called main.cpp, then run clang++ main.cpp then run ./a.out and it should work. If that works then you know you have an issue somewhere else.

    My guess is your file isn't open. Add:

    if (!myfile.good())  {
       cout << "Failed to open file" << endl;
       return 1;
    }
    

    To your code. That should print a failure if it's not open.

    Another possibility is your text file is bad, try a different file, create one by doing:

    echo "hello world" > test.txt
    

    This will overwrite text.txt though if you have one so be careful. Then use that as your test file.



  • @ambershark said in ifstream not working in Qt 5.8 with clang on Mac:

    @Amir_P said in ifstream not working in Qt 5.8 with clang on Mac:

    @ambershark I'm trying to learn c++ at first so I don't want to use Qt frameworks. you told I'm reading file as a binary but in the second code I posted I'm not doing that but I'm getting the same result. the file is exist and I opened it using terminal with the same directory and everything was just fine. In your code you're addressing your file as "main.cpp". how can this be opened? it's just files name not a directory.
    thanks

    It is because main.cpp was in the same directory. It's called relative path. So it will use the current working directory if you don't specify a path.

    What you are using is an absolute path.

    Try putting my code into a file called main.cpp, then run clang++ main.cpp then run ./a.out and it should work. If that works then you know you have an issue somewhere else.

    My guess is your file isn't open. Add:

    if (!myfile.good())
       cout << "Failed to open file" << endl;
       return 1;
    

    To your code. That should print a failure if it's not open.

    Another possibility is your text file is bad, try a different file, create one by doing:

    echo "hello world" > test.txt
    

    This will overwrite text.txt though if you have one so be careful. Then use that as your test file.

    Now I've created a file using that terminal command and changed my code to this

    #include <QCoreApplication>
    #include <iostream>
    #include <fstream>
    #include <sstream>
    
    using namespace std;
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        ifstream myfile;
        myfile.open("/Users/amir_p/Desktop/Untitled.txt");
        string line;
        if (!myfile.good()) {
           cout << "Failed to open file" << endl;
           return 1;
        }
        cout<<myfile.is_open();
        getline(myfile,line);
        myfile.close();
        cout<<line;
        return a.exec();
    }
    

    but after build and run terminal is clear again. what else can I do?


  • Moderators

    @Amir_P said:

    I'm trying to learn c++ at first so I don't want to use Qt frameworks

    Why are you using QCoreApplication then? ;-)

    terminal is clear

    std::cout is buffered; if you want to make sure that the output gets printed to the terminal immediately, then you need to flush the device, e.g. with std:endl.



  • @Wieland said in ifstream not working in Qt 5.8 with clang on Mac:

    @Amir_P said:

    I'm trying to learn c++ at first so I don't want to use Qt frameworks

    Why are you using QCoreApplication then? ;-)

    terminal is clear

    std::cout is buffered; if you want to make sure that the output gets printed to the terminal immediately, then you need to flush the device, e.g. with std:endl.

    thanks that solved. I think it's a problem with clang because in gcc it doesn't need to be flushed every time to print in terminal


  • Moderators

    @Amir_P Clang and gcc most definitely flush differently. I didn't even think about the fact you were locking up in app.exec() with no chance for that to flush.

    Good catch @Wieland! :)


  • Moderators

    @Amir_P Oh I see why you had flushing issues, you didn't use endl in your streams.

    endl not only adds a newline \n but it also flushes. You should be able to add << endl and have it flush for you at some point.

    Edit: oops @Wieland said exactly this.. I should have read his whole post instead of skimming it.. ;)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.