我想將矢量v的內容寫入文件。問題是不是內容,而是地址將放置在文本文件中。將矢量數據寫入文件的C++問題
當我寫* & POS機的POS就地我得到的錯誤:錯誤C2679:二進制「< <」:沒有操作員發現這需要型「進入」
怎樣的右手操作數它工作正確嗎?
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <limits>
/*
Data.txt
John
6543
23
Max
342
2
A Team
5645
23
*/
struct entry
{
// passengers data
std::string name;
int weight; // kg
std::string group_code;
};
void reservations(std::vector<entry> v)
{
std::ofstream outfile;
outfile.clear();
outfile.open("reservations.txt");
// print data in vector
std::vector<entry>::iterator pos;
for (pos = v.begin(); pos!= v.end();++pos)
{
outfile << &pos << std::endl;
std::cout << &pos << std::endl;
}
outfile.close();
}
entry read_passenger(std::ifstream &stream_in)
{
entry passenger;
if (stream_in)
{
std::getline(stream_in, passenger.name);
stream_in >> passenger.weight;
stream_in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::getline(stream_in, passenger.group_code);
stream_in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return passenger;
}
return passenger;
}
int main(void)
{
std::ifstream stream_in("data.txt");
std::vector<entry> v; // contains the reservations
std::vector<entry> a; // contains the cancellations
const int limit_total_weight = 10000; // kg
int total_weight = 0; // kg
entry current;
while (stream_in)
{
current = read_passenger(stream_in);
if (total_weight + current.weight >= limit_total_weight)
{
// push data (cancellations) to vector
a.push_back(current);
}
else
{
total_weight = total_weight + current.weight;
// push data (reservations) to vector
v.push_back(current);
}
}
reservations(v); // write reservations to file
std::cout << "Rest " << limit_total_weight - total_weight << "kg" <<std::endl;
return 0;
}
對不起,但我是一個C++初學者!你的意思是什麼? – burner007