【发布时间】:2011-04-05 23:29:39
【问题描述】:
我在将控制台窗口中创建的文本文件保存到用户输入定义的自定义位置时遇到了一些问题。我希望它采用字符串filepath,这将是保存位置,并将它与字符串filename结合起来,这将是用户选择的文本文件的名称。比如这个C:\users\bobbert\desktop\c++.txt 然后我想要第三个字符串,它是要写入 c++.txt 文件的实际文本。这是我的代码:
cout<<"Please enter a name for your file: "<<endl;
cin>>filename;
cout<<"Please enter a directory to save your file in: "<<endl;
cin>>filepath;
//user is now typing data into the text file
cin>>data;
//the data is now being grabbed and put into the "Data" string
FILE * pFile;
pFile = fopen (filepath.c_str() + filename.c_str(),"a");
//trying to combine the users selected directory + the selected filename here
if (pFile!=NULL)
{
fputs(data.c_str(), pFile);
//here i am trying to take the data of the .txt file
//string and put it into the new file
}
fclose (pFile);
感谢您抽出宝贵时间阅读本文! :)
【问题讨论】:
-
为了将来的参考,最好能真正描述出问题所在。你从来没有在这里问过问题。
标签: c++ string file text user-input