将文件保存到C ++中用户输入定义的位置和名称

时间:2011-04-05 23:29:40

标签: c++ string file text user-input

我在将控制台窗口中创建的文本文件保存到由用户输入定义的自定义位置时遇到一些问题。我希望它采用字符串filepath作为保存位置,并将其与字符串filename结合使用,字符串C:\users\bobbert\desktop\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 ++ .txt文件的实际文本。这是我的代码:

{{1}}

感谢您花时间阅读本文! :)

3 个答案:

答案 0 :(得分:5)

filepath.c_str() + filename.c_str()不会连接字符串,因为它们是指向字符数组的指针,而不是C ++ std::string对象。你只是[尝试]对指针进行算术运算。

尝试:

std::string filename, filepath, data;

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
ofstream fs((filepath + "/" + filename).c_str(), ios_base::app);

//trying to combine the users selected directory + the selected filename here
if (fs)
   fs << data;

我已将C样式fopen的使用替换为C ++流对象,修复了字符串问题并在filepathfilename之间添加了反斜杠(为了安全起见)用户不写它。)

请注意,在将完成的路径传递给.c_str()的构造函数时,仍然需要对std::string连接结果执行ofstream,因为iostreams是在字符串之前设计的图书馆。这只是一个讨厌的C ++ - 主义。

答案 1 :(得分:1)

真正的c ++精神

#include <iostream>
#include <fstream>
#include <string>

int main()
{
    std::string filename, filepath, data;

    std::cout << "Please enter a name for your file: " << std::endl;
    std::cin >> filename;

    std::cout <<" Please enter a directory to save your file in: " << std::endl;
    std::cin >> filepath;

    std::ofstream file((filepath + "/" + filename).c_str());

    //std input is being copied to the file
    file << std::cin.rdbuf();
    file << std::flush;
    file.close();

    return 0;
}

在C精神中结合路径

{
    char* fspec;
    if (-1 == asprintf(&fspec, "%s/%s", filepath.c_str(), filename.c_str()))
        { perror("asprintf"); return 255; }
    std::cout << fspec << std::endl;
    free(fspec);
}

我不清楚你将如何处理输入; 如果您愿意,可以通过多种方式使用字符串流将其读取到内存缓冲区,例如: 不丢空格:

std::stringstream ss;
ss << std::cin.rdbuf();

// OR
std::copy(std::istreambuf_iterator<char>(std::cin) ,
        std::istreambuf_iterator<char>(),
        std::streambuf_iterator<char>(ss));

....以及一些确实删除空格的替代方案:

std::copy(std::istream_iterator<std::string>(std:: cin),
        std::istream_iterator<std::string>(),
        std::stream_iterator<std::string>(ss));

bool my_isspace(char c) { return std::isspace(c); } // in namespace scope
std::remove_copy_if(std::istreambuf_iterator<char> (std::cin),
        std::istreambuf_iterator<char>(),
        std::streambuf_iterator<char>(ss), my_isspace);

答案 2 :(得分:0)

用户是否使用反斜杠终止输入字符串?如果没有,那么你的道路是错误的。