reading a column from a text file

Hi guys.Here is the part of code i use to read and store every value including spaces from a line


std::string line;
std::ifstream Host("Host.txt");
while (std::getline(Host, line))
{
double value;
std::istringstream iss(line);

while((value=iss.get()), iss)
{
save.push_back(value);
}
}

Im trying to make it work to read valuas from the first column of a text file including spaces and i cant get it done.
Right now all the values are on the first line and some of them are separated by spaces so save[0] is the first number from the first line,save[1] the second and so on.And im trying to make it work if i transpose all those values from first line to the first column.
Thanks
How does the file look ?
Well,if i open it with excel it has only column A with numbers on each row ,and sometimes there are empty cells on column A
1
2
"space"
1
3
etc
That is how i want it to be.When i made the program the values were like this:
1 2 "space" 1 3 on the first row and the program above worked with the second condiguration but not with the first configuration
How are columns separated? Is it spaces?

One way to read the values is to read each line, then pick out the numbers one at a time, these then form your columns.

For example, assuming whitespace separators and the double values that you used:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
typedef std::vector<double> row_type;
typedef std::vector<row_type> grid_type;

row_type read_row(std::istream& is)
{
    row_type row;

    double value;
    while (is >> value)
        row.push_back(value);

    return std::move(row);
}

grid_type read_grid(std::istream& is)
{
    grid_type grid;

    std::string line;
    while (std::getline(is, line))
    {
        std::istringstream iss(line);
        row_type row = read_row(iss);
        grid.push_back(row);
    }

    return std::move(grid);
}

grid_type read_grid(std::string filename)
{
    std::ifstream feed(filename);
    return std::move(read_grid(feed));
}


You can pull out columns from that, or you can modify the example to read a specific column.

To pull out a column, you could do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
typedef row_type column_type;

column_type extract_column(const grid_type& grid, size_t col)
{
    if (grid.empty() || col >= grid[0].size())
        throw std::range_error("col too large");

    column_type column;
    column.resize(grid.size());
    for (const row_type& row : grid)
        column[idx++] = col < row.size() ? row[col] : NAN; // or 0.0

    return std::move(column);
}
Last edited on
Thank you.But isnt there a simple solution i mean i only want to read and store every element only from column A and thats it, it doesnt matter what i do next with that vector of elements(btw my text file is basically an excel file but saved in a format.txt).and im supposed and im trying to make it with few lines of code
isnt there a simple solution i mean i only want to read and store every element only from column A and thats it

Well, if this is the input file,
1
2
"space"
1
3
etc
then this should suffice:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

int main()
{
    std::vector<std::string> columnA;
    std::ifstream fin("Host.txt");
    
    for (std::string line; getline(fin, line);  )
        columnA.push_back(line);        
}

... or is there something I'm missing?
Topic archived. No new replies allowed.