如何解决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;
for (int count = 1; !file.eof(); ++count)
{
getline(file,line);
cout << line << endl;
if (count % 24 == 0) system("Pause");
}
system("Pause");
}
以下是我的错误
[错误]类型为'const char [34]'和'const char *'的无效操作数到二进制'operator +'
更新1
按照@drescherjm注释。我已经更新了代码
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。