Notice: Undefined offset: 8983374 in /var/www/html/qa-external/qa-external-users.php on line 744
Given a file ``data.txt`` with three columns of data separated by spaces, read it into one complex sequence. - OnlineGDB Q&A
Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Given a file ``data.txt`` with three columns of data separated by spaces, read it into one complex sequence.

+3 votes
asked Nov 4, 2019 by anonymous

1 Answer

+1 vote
answered Jun 3, 2025 by Jerry Jeremiah (2,040 points)

This doesn't do any error checking, but it does read the data:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

struct row_t {
    std::string x, y, z;
};

std::vector<row_t> table;

int main()
{
    std::ifstream inputFile("data.txt");

    row_t row;
    while (inputFile >> row.x >> row.y >> row.z)
        table.push_back(row);

    for(const row_t& row : table)
        std::cout << row.x << " " << row.y << " " << row.z << "\n";

    return 0;
}

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...