Dear friends:
i have a data file, the structure of the file is as follows:
// data.txt
1 1 2.5
2 2 1.0 2.0
3 2 3.0 4.0
4 4 1.0 3.2 2.7 1.6


In the data.txt, the fisrt column is the row number, and the second column is the number of values behind it.
How to read in it with a loop. which input command should i use.

6 Years
Discussion Span
Last Post by Branickiod

See, I don't understand this

and the second column is the number of values behind it.

but I see you have more than two columns.

here's a suggestion:

open file for read
string c
while(getline(fin, c))
    read having ' '(space) as delimitter till '\n'
    @ process every element: 1st - the row
    @                        2nd - the column etc.

here's a sample of how you can read from a file a matrix.

#include <sstream>
#define MAX 100
ifstream f_in("filename.txt", ios::in);
char Mat[MAX][MAX];
int DimX, DimY;

void read(){
    f_in >> DimX >> DimY;
    for(int i=0;i<DimX;++i)
        f_in >> Mat[i];

Having a sample of filename.txt:
4 5
Where 4 will be the DimX, and 5 DimY: Mat[DimX=4][DimY=5]
Mat[3][2]='g' for example

Edited by Lucaci Andrew


What have you tried so far? Show us your code - or just some pseudo-code - and we'll help you debug it.

If you aren't sure where to start, start by figuring out how you are going to read in a given line of text and parse it. I recommend using the getline() method of your input stream, saving the line into a string, and then breaking it up into pieces using a stringstream. This would make processing the file line by line much easier.

Are the $Elements and $EndElements tags part of the actual file format? If so, I would start by scanning for $Elements, after which you would loop on each line, testing to see if you have found $EndElements.

If it is allowable, I would recommend using a vector of vectors of double to hold the data:

vector< vector<double> > table;

I suspect, however that this would not be allowable under the rules of the project, which presumably is to teach you how to write data structures for handling unevenly sized data on your own. If that's the case, you'll want to write a linked list class, with each link representing a row, and the data columns inside the list nodes as a dynamically allocated array of double.

class TableRow
    unsigned int row_number;
    size_t row_size;
    double* columns;
    TableRow* next;

    TableRow(unsigned int row, size_t size) : row(row_number), size(row_size)
        next = 0;   // initialize next to null
        columns = new double[row_size];
    // the rest is left as an exercise

This would let you create the rows as needed and add them to the table.


See, I don't understand this

Dear friends:
thank you very much for you help. that means if the second column is 2, then there are 2 double values behind it.
if it is 4, then there are 4 double values behind it.


Thank you so much for this coding actually i m waiting for this ans and i got this one........
Can we get this output from other kind of coding..............???

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.