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. Failed to open file when I use fstream
Forum Update on Monday, May 27th 2025

Failed to open file when I use fstream

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 5 Posters 6.8k 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
    PsionicAlch
    wrote on last edited by PsionicAlch
    #1

    I'm new to Qt Creator so I'm not quite sure how the filing of it works... I created a simple fstream program that reads the text inside a .txt file and displays it in the terminal. The issue is that it doesn't seem to be able to find the .txt file...

    main.cpp:

    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main(){
        ifstream myFile;
        string name;
        myFile.open("Test.txt");
        if(myFile.is_open()){
            getline(myFile, name);
            cout << name << endl;
            myFile.close();
        }else{
            cout << "Failed to open file!" << endl;
        }
    
        return 0;
    }
    

    File_Import_Test.pro:

    TEMPLATE = app
    CONFIG += console c++11
    CONFIG -= app_bundle
    CONFIG -= qt
    
    SOURCES += \
            main.cpp
    
    DISTFILES += \
        Test.txt
    
    

    Please help a noob out.

    2_1554577215803_Test_Dot_TXT_file.png
    1_1554577215803_Main_Dot_CPP_File.png
    0_1554577215803_Dot_Pro_File.png

    0_1554577295272_Terminal.png

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi and welcome to the forums
      Doing myFile.open("Test.txt");
      will make it look in current folder which normally would be in the build folder where the .exe file is.
      Since the file is in the project folder, it wont find it.
      It does not know where the project folder is.

      Do you want the file to be embedded into the exe as a resource or be a free file you load at startup ?
      Since you are using ifstream, i assume free file as only QFile will load as ressource.

      You can place the text.file next to the exe and do
      myFile.open(qApp->applicationDirPath() +"/Test.txt"); // note might be issue with / (qt) and \
      if you can , use QFile.

      The build folder you can see in the "projects" button to the left.

      1 Reply Last reply
      3
      • P Offline
        P Offline
        PsionicAlch
        wrote on last edited by
        #3

        I want the .txt file to be a free file that I load up at startup.

        I have trouble using QFile. The project I created is just a plain C++ project.

        I copied the .txt file over to the build folder and now it works but that's a little frustrating... Why doesn't Qt Creator also copy the txt file over?

        I get errors when I use myFile.open(qApp->applicationDirPath() +"/Test.txt");

        Thanks for your help I appreciate it, QT Creator is a little different from the IDEs I'm used too(>_<).

        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @PsionicAlch said in Failed to open file when I use fstream:

          Why doesn't Qt Creator also copy the txt file over?

          Because you don't tell the build system to do so. QtCreator or anyone else don't do anything you did not tell them...

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          P 1 Reply Last reply
          5
          • Christian EhrlicherC Christian Ehrlicher

            @PsionicAlch said in Failed to open file when I use fstream:

            Why doesn't Qt Creator also copy the txt file over?

            Because you don't tell the build system to do so. QtCreator or anyone else don't do anything you did not tell them...

            P Offline
            P Offline
            PsionicAlch
            wrote on last edited by
            #5

            @Christian-Ehrlicher Ooooooo so how do you tell the build system to copy it over to the build folder? (0_0)

            Cobra91151C jsulmJ 2 Replies Last reply
            0
            • P PsionicAlch

              @Christian-Ehrlicher Ooooooo so how do you tell the build system to copy it over to the build folder? (0_0)

              Cobra91151C Offline
              Cobra91151C Offline
              Cobra91151
              wrote on last edited by Cobra91151
              #6

              @PsionicAlch

              Hi! First of all if you want to tell something to the build system, example Qt Creator, you should add the appropriate command to the .pro file, so it will manage for you the pre/post build steps.

              Also, you can create the file using C++:

                  QFile myFile(QApplication::applicationDirPath() + "/Test.txt");
                  myFile.open(QIODevice::ReadWrite);
              
                  if (myFile.isOpen()) {
                      qDebug() << "File is open!";
                  } else {
                      qDebug() << "Failed to open the file!";
                  }
              
                  myFile.close();
              

              It should create the file in your application directory on the runtime.

              1 Reply Last reply
              1
              • P PsionicAlch

                @Christian-Ehrlicher Ooooooo so how do you tell the build system to copy it over to the build folder? (0_0)

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @PsionicAlch Take a look at https://doc.qt.io/qt-5/qmake-variable-reference.html#installs
                You should also consider using Qt resource files https://doc.qt.io/qt-5/resources.html

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

                1 Reply Last reply
                3

                • Login

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