Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using ifstream on a Mac
QtWS25 Last Chance

Using ifstream on a Mac

Scheduled Pinned Locked Moved Unsolved General and Desktop
stdio
8 Posts 5 Posters 3.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.
  • P Offline
    P Offline
    pacostacos
    wrote on 28 Jun 2016, 05:21 last edited by
    #1

    Hi guys, I'm trying to access a file in the executable's directory. The file does not open with the code provided

    std::ifstream in;
      in.open(fn.toStdString(), std::ios_base::binary | std::ios_base::in);
    
    
      msgBox.setText(QFileInfo(".").absolutePath());
      msgBox.exec();
    

    the message box outputs
    "/Users/me/Qt/QtProjects/build-Desktop-Debug/first.app/Contents"

    which leads me to believe that ifstream is using the wrong directory. I have a test file under "/Users/me/Qt/QtProjects/build-Desktop-Debug/" . What should I do to access the directory of my executable? Thanks!

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 28 Jun 2016, 06:13 last edited by
      #2

      Hi!

      What is fn?
      See http://doc.qt.io/qt-5/qcoreapplication.html#applicationDirPath

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pacostacos
        wrote on 28 Jun 2016, 08:48 last edited by
        #3

        @pacostacos said:

        fn is a parameter, type QString

        J 1 Reply Last reply 28 Jun 2016, 09:06
        0
        • P pacostacos
          28 Jun 2016, 08:48

          @pacostacos said:

          fn is a parameter, type QString

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 28 Jun 2016, 09:06 last edited by
          #4

          @pacostacos I mean what is its content? I guess it is the path to the file you want to open?
          And please check the link I provided: this way you can get the path to the directory where your executable is residing

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • P Offline
            P Offline
            pacostacos
            wrote on 28 Jun 2016, 17:50 last edited by
            #5

            Yes, fn stands for filename. it is the path to the file I want to open.
            Using QCoreApplication::applicationDirPath(), I obtain
            "/Users/me/Qt/QtProjects/build-Desktop-Debug/first.app/Contents/MacOS"

            I have a test file under "/Users/me/Qt/QtProjects/build-Desktop-Debug/" named test.pdf that I want to open. The first.app is in that folder.

            I actually just added the file to first.app/MacOS/ and it worked.... How do I include files in QT so they end up over there? thanks

            M 1 Reply Last reply 29 Jun 2016, 12:11
            0
            • R Offline
              R Offline
              Rondog
              wrote on 28 Jun 2016, 19:46 last edited by Rondog
              #6

              The OS X applications (*.app) are actually folders but treated in a unique way. The function applicationDirPath() is actually returning the correct folder which is a sub folder of the application bundle where the real executable lives.

              I use this to deal with this situation:

              	QDir								data_location;
              	QString								data_path;
              	
              	data_path = qApp->applicationDirPath();
              	
              #ifdef Q_OS_MAC
              	int index = data_path.lastIndexOf(".app/");
              	data_path.truncate(index);
              	
              	index = data_path.lastIndexOf('/');
              	data_path.truncate(index);
              #endif
              	
              	data_location.setPath(data_path);
              

              The variable 'data_path' ends up with the location of the executable (non-OSX) or the folder of the .app bundle (OSX).

              One thing that I just realized looking at this code is that it will fail if the extension '.app' is not lower case (if that is even possible?). Maybe it should be something like this instead:

              int index = data_path.lastIndexOf(".app/",-1,Qt::CaseInsensitive);
              
              1 Reply Last reply
              1
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 28 Jun 2016, 22:57 last edited by
                #7

                Hi,

                The quick and dirty way to do such testing is to add CONFIG -= app_bundle so you don't have the application bundle created and you'll be able to run your test.

                Just a side note: on OS X you should not have external resources in the same folder as the executable. It should be in folder name Resources in Contents.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • P pacostacos
                  28 Jun 2016, 17:50

                  Yes, fn stands for filename. it is the path to the file I want to open.
                  Using QCoreApplication::applicationDirPath(), I obtain
                  "/Users/me/Qt/QtProjects/build-Desktop-Debug/first.app/Contents/MacOS"

                  I have a test file under "/Users/me/Qt/QtProjects/build-Desktop-Debug/" named test.pdf that I want to open. The first.app is in that folder.

                  I actually just added the file to first.app/MacOS/ and it worked.... How do I include files in QT so they end up over there? thanks

                  M Offline
                  M Offline
                  micland
                  wrote on 29 Jun 2016, 12:11 last edited by
                  #8

                  @pacostacos said:

                  I actually just added the file to first.app/MacOS/ and it worked.... How do I include files in QT so they end up over there? thanks

                  Depending on what you want to achieve you can integrate the file as resource into your Qt application using the Qt resource system and access the file via qrc:/... so it will be available on all platforms via the same way (but only from inside our application).
                  See http://doc.qt.io/qt-5/resources.html

                  1 Reply Last reply
                  0

                  4/8

                  28 Jun 2016, 09:06

                  • Login

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