【问题标题】:Adding vector elements into a data file将矢量元素添加到数据文件中
【发布时间】:2016-12-16 05:25:16
【问题描述】:

我已经要求用户输入几个字符串元素来创建一个字符串向量,并希望将它们打印到一个数据文件中。

fstream file_name;
string user_input_file;
vector<vector<string>>read_in_vector;

file_name.open(user_input_file.c_str());

cout << "You have entered:" << endl;
        cout << "Row Desc:  " << add_row << endl;
        cout << "Username:  " << add_username << endl;
        cout << "Password:  " << add_password << endl;
        cout << "Notes: " << add_notes << endl << endl;
vector<string>temp = { add_row,add_username,add_password,add_notes };

我将如何添加名为 temp 的向量元素 进入数据文件而不擦除数据文件的现有部分

【问题讨论】:

    标签: c++ vector


    【解决方案1】:

    使用 fstream::operator<<,您可以附加到文件/流/缓冲区/任何内容的末尾,如果我正确阅读,我认为这就是您想要的。用法:

    file_name << temp[0];
    

    这将安全地将文本附加到文件中,而不会修改其其余内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-13
      • 2020-08-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多