3

I have file like this:

int1--tab--int2--tab--int3--tab--int4--tab--newline

int1--tab--int2--tab--int3--tab--int4--tab--newline

int1--tab--int2--tab--int3--tab--int4--tab--newline ...

I want to save each row in to an array. I mean all int1 in to an array and want to do the same whit int2 int3 ...

I realy dont know how to do it please help me

I already try read it line by line

#include <sstream>
#include <string>

std::string line;
while (std::getline(infile, line))
{
    std::istringstream iss(line);
    int a, b;
    if (!(iss >> a >> b)) { break; } 

}
2
  • Not a bad attempt. Do you know there are always 4 columns? Do you know about vector<int> ? Commented Nov 14, 2013 at 22:27
  • is the "--" in your file a separator you added to make it clear, or are there really "--" in the file? Commented Nov 14, 2013 at 22:31

3 Answers 3

3

You had the right idea using a stringstream. Since code to read delimited files is likely to be used again, you may find it useful to put this into class. Here's an excerpt from my personal delimited FileReader class:

bool FileReader::getrow(RowMap &row){
    std::string line = "";
    if(std::getline(filehandle,line)){
        std::stringstream line_ss(line);
        std::string column = "";
        unsigned int index = 0;
        while(std::getline(line_ss,column,delimiter)){
            if(index < headers.size()){
                row[headers[index]] = column;
                index++;
            }
            else{
                break;
            }
        }
        return true;
    }
    return false;
}

Where RowMap is a typedef of:

typedef std::unordered_map<std::string,std::string>

And headers is a typedef of:

typedef std::vector<std::string> RowHeadersVector;

And should have your column names:

RowHeadersVector headers;
headers.push_back("column_1");

In my example, I'm using a map of string to string, but you could easily change it to:

typedef std::unordered_map<std::string,int>

The benefit of using a map like this is self documented code:

row["column_1"]
Sign up to request clarification or add additional context in comments.

Comments

1

This looks quite reasonable. If you know the number of columns, you can just create suitable arrays and add them:

std::vector<int> array, barray, carray, darray;
std::istringstream lin;
for (std::string line; std::getline(infile, line); ) {
    lin.clear();
    lin.str(line);
    if (lin >> a >> b >> c >> d) {
        aarray.push_back(a);
        barray.push_back(b);
        carray.push_back(c);
        darray.push_back(d);
    }
    else {
        std::cout << "WARNING: failed to decode line '" << line << "'\n";
    }
}

Comments

0

It's strange that you've done the hard part and are stuck on what seems to me to be the easy part.

Just replace a and b with arrays and an index.

int a[100], b[100];
int count = 0;
std::string line;
while (std::getline(infile, line))
{
    std::istringstream iss(line);
    if (!(iss >> a[count] >> b[count])) { break; } 
    ++count;
}

At the end of this loop count will tell you how many items you've added to the arrays. Of course this code will crash if you try to add more than 100 items to the arrays. I'll leave you to think about that.

1 Comment

What if a whitespace appears in a column?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.