我遇到了一个简单而烦人的问题。
如果文件名是硬编码或通过字符串变量分配的,则我有以下代码-
//This function will get filenames from a .txt file
//Then call getDataFromFile() with filename as argument
void getFileNames(string dataFileName){
string line;
ifstream dataFile(dataFileName);
if(!dataFile){
cout << "Error! No such file found! Ending Program." <<
endl;
exit(0);
}
while(getline(dataFile,line)){
if(!line.empty()){
getDataFromFile(line);
}
}
}
//**Issue is inside this function**
void getDataFromFile(string fileName){
//Files are under "data/" path -
//To access Jan.csv - "data/Jan.csv"
string filePath = "data/Jan.csv"; //Works
string filePath = "data/"+fileName; //Not working - Unable to find file
ifstream dataFile(filePath);
//...Rest of code
}
下面是.txt文件的内容
Jan.csv
Feb.csv
我尝试使用
将字符串作为c字符串传递ifstream dataFile(filePath.c_str());
,仍然无法找到该文件。