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 (将#修改为@)