I would like to read a text file, which contains the following data:
Wogger John 2 6.2
Bilbo 111 81.3
Mary 29 154.8
The data separated by tabulator. The problem is if the string contains a space (for example: 'Wogger John'), then the program doesn't work. THe program does work if I replace string 'Wogger John' to 'Wogger' or 'John'. How to fix the problem? How to use the getline() function. Here the code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cerrno>
#include <cstring>
#include <tuple>
std::vector<std::tuple<std::string, int, double>> Readfile()
{
std::ifstream File("file_read_v3.txt");
std::vector<std::tuple<std::string, int, double>> data;
std::string name;
int a;
double b;
while (File >> name >> a >> b)
{
data.push_back(std::tuple<std::string, int, double>(name, a, b));
}
return data;
}
int main()
{
auto vt = Readfile();
for (const auto& i : vt) {
std::cout << std::get<0>(i) << ", " << std::get<1>(i) << ", " << std::get<2>(i) << std::endl;
}
return 0;
system("pause");
}