Failed to open file when I use fstream



  • 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


  • Lifetime Qt Champion

    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.



  • 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(>_<).


  • Qt Champions 2018

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



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



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


  • Qt Champions 2018


Log in to reply
 

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