我用 QT creator 4.1.0 创建了一个 C++ 控制台应用程序这个应用程序最初是用代码块创建的,并且工作正常。
这是代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string nomeFile="";
cout << "\nNome file: ";
cin >> nomeFile; // inserire nome file ROF.txt da leggere
//ifstream leggiROF(nomeFile.c_str()); // apre in lettura il file ROF.txt
ifstream leggiROF("C:\\Users\\Massimo Di Natale\\Documents\\Programmi C++ 11\\Programmi_QT\\prova\\a.txt");
string riga="";
if(leggiROF) // se lo trova
{
while(!leggiROF.eof()) // finché non raggiunge la fine del file
{
getline(leggiROF, riga); // legge la riga
cout << "Riga: " << riga << endl;
} // end while
} // end if
else // altrimenti
{
cout << "\nIl nome file inserito non e' stato trovato!!!" << endl;
cout << "Controllare che sia stato inserito nella cartella corretta.\n" << endl;
exit (EXIT_SUCCESS); // termina il programma
} // end else
leggiROF.close();
return 0;
}
如果我在 ifstream 上指定文件名及其路径,它就可以工作。如果我想通过 cin 插入文件名,然后使用 ifstream filenam.c_str() 它找不到文件。我想从目录中读取 .txt 文件是项目还是 .exe 是