C++ C++;用文件名追加路径
我正在尝试附加保存文件的路径以及文件名。下面是我的代码C++ C++;用文件名追加路径,c++,string,fstream,dev-c++,C++,String,Fstream,Dev C++,我正在尝试附加保存文件的路径以及文件名。下面是我的代码 int main() { cout << "Please enter the name of the file: "; string fileName; getline(cin, fileName); ifstream file("C:\\Users\\Faisal\\Desktop\\Programs\\"+fileName.c_str(), ios::in); string line
int main()
{
cout << "Please enter the name of the file: ";
string fileName;
getline(cin, fileName);
ifstream file("C:\\Users\\Faisal\\Desktop\\Programs\\"+fileName.c_str(), ios::in);
string line;
for (int count = 1; !file.eof(); ++count)
{
getline(file, line);
cout << line << endl;
if (count % 24 == 0) system("Pause");
}
system("Pause");
}
c字符串没有+运算符。您的代码正试图添加指针。@drescherjm否我想从路径打开我的文件使用std::string变量进行字符串连接。+将在那里工作。@drescherjm
ifstream文件(“C:\\Users\\Faisal\\Desktop\\Programs\\”+std::string(fileName.C_str()),ios::in)
给我[错误]没有匹配的函数调用'std::basic\u ifstream::basic\u ifstream(std::basic\u string,const openmode&')
你确定文件不是ali.txt
?
int main()
{
cout << "Please enter the name of the file: ";
string fileName;
string pathName = "C:\\Users\\Faisal\\Desktop\\Programs\\";
getline(cin, fileName);
string fileToOpen = pathName + fileName;
ifstream file(fileToOpen.c_str());
if (!file){
// There was an error so display an error
// message and end the PROGRAM.
cout << "Error opening " << fileName << endl;
exit(EXIT_FAILURE);
}
string line;
for (int count = 1; !file.eof(); ++count)
{
getline(file, line);
cout << line << endl;
if (count % 24 == 0) system("Pause");
}
system("Pause");
}
Please enter the name of the file: ali
Error opening ali