Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. ifstream not working in Qt 5.8 with clang on Mac
QtWS25 Last Chance

ifstream not working in Qt 5.8 with clang on Mac

Scheduled Pinned Locked Moved Solved C++ Gurus
9 Posts 3 Posters 4.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Amir_P
    wrote on last edited by
    #1

    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();
    }
    
    A 1 Reply Last reply
    0
    • A Amir_P

      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();
      }
      
      A Offline
      A Offline
      ambershark
      wrote on last edited by
      #2

      @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++. ;)

      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

      A 1 Reply Last reply
      1
      • A ambershark

        @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++. ;)

        A Offline
        A Offline
        Amir_P
        wrote on last edited by
        #3

        @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

        A 1 Reply Last reply
        0
        • A Amir_P

          @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

          A Offline
          A Offline
          ambershark
          wrote on last edited by kshegunov
          #4

          @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.

          My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

          A 1 Reply Last reply
          1
          • A ambershark

            @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.

            A Offline
            A Offline
            Amir_P
            wrote on last edited by
            #5

            @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?

            ? 1 Reply Last reply
            0
            • A Amir_P

              @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?

              ? Offline
              ? Offline
              A Former User
              wrote on last edited by A Former User
              #6

              @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.

              A 1 Reply Last reply
              4
              • ? A Former User

                @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.

                A Offline
                A Offline
                Amir_P
                wrote on last edited by
                #7

                @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

                A 2 Replies Last reply
                0
                • A Amir_P

                  @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

                  A Offline
                  A Offline
                  ambershark
                  wrote on last edited by
                  #8

                  @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! :)

                  My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                  1 Reply Last reply
                  0
                  • A Amir_P

                    @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

                    A Offline
                    A Offline
                    ambershark
                    wrote on last edited by ambershark
                    #9

                    @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.. ;)

                    My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved