I just want to read a text file and store the data into a vector. Thereby the value weight should be sum till the limit is reached. The first four lines will be read correctly but the following will not read. What is the mistake?
#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::ofstream file;
file.clear();
file.open("reservations.txt");
file.close();
}
entry read_passenger(std::ifstream &stream_in)
{
entry passenger;
if (stream_in)
{
std::getline(stream_in, passenger.name);
stream_in >> passenger开发者_StackOverflow中文版.weight;
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 passengers data
const int limit_total_weight = 10000; // kg
int total_weight = 0; // kg
entry current;
while (!stream_in.eof())
{
current = read_passenger(stream_in);
total_weight = total_weight + current.weight;
std::cout << current.name << std::endl;
if (total_weight >= limit_total_weight)
{
break;
}
}
return 0;
}
These two lines,
std::getline(stream_in, passenger.group_code);
stream_in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
should be in the opposite order:
stream_in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::getline(stream_in, passenger.group_code);
Think about what the purpose of the ignore
is.
Also, instead of checking only for EOF, check for general error.
I.e., instead of
while (!stream_in.eof())
write
while (stream_in)
Maybe there's more wrong, but the above is what I saw immediately.
Cheers & hth.,
I try not to mix formatted and line-oriented input for precisely this reason. If it were me, and I had to use getline
anywhere, I would use getline
everywhere:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <limits>
#include <sstream>
struct entry
{
// passengers data
std::string name;
int weight; // kg
std::string group_code;
};
void reservations()
{
std::ofstream file;
file.clear();
file.open("reservations.txt");
file.close();
}
std::istream& operator>>(std::istream& stream_in, entry& passenger)
{
std::getline(stream_in, passenger.name);
std::string weightString;
std::getline(stream_in, weightString);
std::istringstream (weightString) >> passenger.weight;
std::getline(stream_in, passenger.group_code);
return stream_in;
}
void read_blankline(std::istream& stream_in) {
std::string blank;
std::getline(stream_in, blank);
}
int main(void)
{
std::ifstream stream_in("data.txt");
std::vector<entry> v; // contains the passengers data
const int limit_total_weight = 10000; // kg
int total_weight = 0; // kg
entry current;
while (stream_in >> current)
{
total_weight = total_weight + current.weight;
std::cout << current.name << std::endl;
if (total_weight >= limit_total_weight)
{
break;
}
read_blankline(stream_in);
}
return 0;
}
精彩评论