简体   繁体   English

将向量写入 output 文件 c++

[英]Writing a vector to a output file c++

Here I have a structure called 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;

I'm trying to write the data inside my vector to a output file.For this i've written the following code我正在尝试将向量中的数据写入 output 文件。为此,我编写了以下代码

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";
}
  
  ostream_iterator<contactDetails> output_iterator(output_file, "\n");
  copy(begin(proContactFile),end(proContactFile), output_iterator);  
}
output_file.close();
fin.close();
remove("Contact.csv");//Deletes contacts.csv file
rename("temp.csv" , "Contact.csv");

But this code always gives me an error.Also I want to write the data to the file with the following way.但是这段代码总是给我一个错误。我还想用以下方式将数据写入文件。

Name,Nick name,Phone number,Carrier,Address

Whats wrong with my code?(basically what i'm doing here is let user to delete a element in the vector and the write that updated vector to a output file)我的代码有什么问题?(基本上我在这里做的是让用户删除向量中的一个元素并将更新后的向量写入 output 文件)

This is the error I am receiving: Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'const _Ty' (or there is no acceptable conversion)这是我收到的错误: Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'const _Ty' (or there is no acceptable conversion)

So using this technique of writing a vector所以使用这种编写向量的技术

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

requires that you overload operator<< for your struct.要求您为您的结构重载operator<< Ie IE

ostream& operator<<(ostream& out, const contactDetails& details)
{
    // your output code here, e.g.
    out<< details.name << ',' << details.nickName << ','; //etc << ...
    return out;
}

But the puzzling issue remains.但令人费解的问题仍然存在。 The code above is for updating a file.上面的代码用于更新文件。 Which implies that you already have a file.这意味着您已经有一个文件。 Which implies that you are already successfully writing that file somewhere else in your code.这意味着您已经在代码的其他地方成功地编写了该文件。 How did you achieve that?你是如何做到的?

However you managed it you should update that other code so that it uses the operator<< that you are going to write to fix this code.无论您如何管理它,您都应该更新其他代码,以便它使用您将要编写的operator<<来修复此代码。 That way all your contact details output goes through one function.这样一来,您所有的联系方式 output 都会通过一个 function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM