【问题标题】:Writing a vector to a file c++将向量写入文件c ++
【发布时间】:2020-12-17 22:32:10
【问题描述】:

这里我有一个叫做contacts的结构

 typedef struct contacts 
    {
       string name;   //{jhonathan , anderson , felicia}
       string nickName; //{jhonny  , andy , felic}
       string phoneNumber; // {13453514 ,148039 , 328490}
       string carrier;  // {atandt , coolmobiles , atandt }
       string address; // {1bcd , gfhs ,jhtd }
    
    } contactDetails;
    
    vector <contactDetails> proContactFile;

我正在尝试将向量中的数据写入输出文件。为此,我编写了以下代码

    ofstream output_file("temp.csv");
    ostream_iterator<contactDetails> output_iterator(output_file, "\n");
    copy(begin(proContactFile),end(proContactFile), output_iterator);

但是这段代码总是给我一个错误。另外我想用下面的方式将数据写入文件。

Name,Nick name,Phone number,Carrier,Address

我的代码有什么问题?

【问题讨论】:

  • 你得到什么错误?
  • @Chipster 它说Error C2679 binary '&lt;&lt;': no operator found which takes a right-hand operand of type 'const _Ty' (or there is no acceptable conversion)

标签: c++ vector struct file-io


【解决方案1】:

std::ostream_iterator&lt;T&gt;T 类型调用 operator&lt;&lt;。您需要为std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const contactDetails&amp; cont) 编写代码,以便ostream_iterator 迭代并写入其输出流。

typedef struct contacts 
{
   string name;   //{jhonathan , anderson , felicia}
   string nickName; //{jhonny  , andy , felic}
   string phoneNumber; // {13453514 ,148039 , 328490}
   string carrier;  // {atandt , coolmobiles , atandt }
   string address; // {1bcd , gfhs ,jhtd }
} contactDetails;
    
vector <contactDetails> proContactFile;

std::ostream& operator<<(std::ostream& os, const contactDetails& cont)
{
    os << cont.name << "," << cont.nickName << ",";
    os << cont.phoneNumber << "," << cont.carrier << ",";
    os << cont.address << endl;
    return os;
}

【讨论】:

  • 这段代码看起来不错。但它给了我一个错误。但我不知道出了什么问题。 Error C2601 'operator &lt;&lt;': local function definitions are illegal
  • @SeektheFreak,它不能是contactDetails 的成员函数。你写了无代码函数吗?
  • 嗯,我刚才试了一下free功能,然后代码无法识别callDetails
  • @SeektheFreak,在我用std::stringstream 测试它在我的电脑上工作后,我发布了答案。你能发布一个可重现的代码吗?
  • @SeektheFreak,顺便说一下,这是contactDetails,而不是callDetails
【解决方案2】:
void deleteContact() //Delete Feature
{
    ofstream output_file("temp.csv");
    int selectContact;
    cout << "Which Contact you want to delete ? Enter the relevent index " << endl;
    cin >> selectContact;
    
    if (selectContact > proContactFile.size()) {
        cout << "Invalid entry";
    }
    else {
        proContactFile.erase(proContactFile.begin() + (selectContact - 1));
        cout << "Delete successfully";
    }
   
    std::ostream& operator<<(std::ostream & os, const contactDetails & cont)
    {
        os << cont.name << "," << cont.nickName << ",";
        os << cont.phoneNumber << "," << cont.carrier << ",";
        os << cont.address << endl;
        return os;
    }
    output_file.close();
}

嘿,现在之前的错误消失了。现在它只是说expected a ';'。但是这里什么都没有。

【讨论】:

  • 请参考我修改过的答案,如果编译代码有问题,再发一个问题。 SO 说通过评论讨论不是一个好主意。
猜你喜欢
  • 2013-03-26
  • 1970-01-01
  • 2023-03-06
  • 2019-10-27
  • 1970-01-01
  • 2016-03-14
  • 1970-01-01
  • 2021-03-20
  • 2013-04-29
相关资源
最近更新 更多