简体   繁体   中英

Cannot read in a txt file using ifstream

I am trying to read in a basic txt file, but I think the programming is not detecting the txt file. Here's my code.

int main() {
   ifstream in;
   in.open("testing.txt");
   if (in.fail()) cout << "fail" << endl;

   return 0;
}

The program is printing out fail. I created the txt file by right clicking the project and adding a new empty file. I am completely stuck, so I would appreciate any help.

As from QT Creator's documentation, you can change the working directory where your program should be executed in the project run settings :

在此处输入图片说明

If your file exists in a different path than the default (which is where QT Creator builds the executable), you can set it there.

I created the txt file by right clicking the project and adding a new empty file.

That creates the .txt file at your project main path, not where the executable is build actually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM