|
-
May 10th, 2012, 07:17 AM
#1
Reading file into array
Hi Guys!
i'm reading a file which has 20 rows and random number of columns, i want to put them in array/vector which i did but the problem is that array is filling a cell with a garbage value at location where i don't have a value in the data file.
suppose i have data file like following (there is tab between each column and each row has different number of columns)
Code:
20 30 10
22 10 9 3 40
60 4
30 200 90
33 320 22
here is my code
Code:
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
int main() {
std::vector<std::vector<int> > allData;
std::ifstream fin("data.dat");
std::string line;
while (std::getline(fin, line)) { // for each line
std::vector<int> lineData; // create a new row
int val;
std::istringstream lineStream(line);
while (lineStream >> val) { // for each value in line
lineData.push_back(val); // add to the current row
}
allData.push_back(lineData); // add row to allData
}
//std::cout << "row 0 contains " << allData[0].size() << " columns\n";
//std::cout << "row 0, column 1 is " << allData[0][1] << '\n';
}
your help will be appreciated
Regards,
Ewa
Last edited by me_newbie; May 10th, 2012 at 07:20 AM.
-
May 10th, 2012, 07:30 AM
#2
Re: Reading file into array
Are you accessing "cells that don't have a value" ?
The following is how you would loop thru this structure:
Code:
for (int i=0; i<allData.size(); ++i)
{
for (int j=0; j<allData[i].size(); ++j)
{
cout << allData[i][j] << " ";
}
cout << "\n";
}
-
May 10th, 2012, 08:25 AM
#3
Re: Reading file into array
 Originally Posted by Philip Nicoletti
Are you accessing "cells that don't have a value" ?
The following is how you would loop thru this structure:
Code:
for (int i=0; i<allData.size(); ++i)
{
for (int j=0; j<allData[i].size(); ++j)
{
cout << allData[i][j] << " ";
}
cout << "\n";
}
Thanks for reply, i modified your code to suit my purpose and now i want to access a specific column in a specific row. how to do it? because i want to read and analyze the 3rd column values.
Code:
int currentPosition=1;
for (int i=0;i<allData.size();i++){
for (int j =0;j<allData[i].size();j++){
if (allData[i][0]==currentPosition){
for (;j<allData[i].size();j++){
cout << "" << allData[i][j] << '\t';
}
cout << "" << endl;
}
}
}
}
Thanks in advance
-
May 10th, 2012, 08:36 AM
#4
Re: Reading file into array
Is this what you want ?
Code:
int column = 2; // 0-based (so 3rd column)
for (int i=0; i<allData.size(); ++i)
{
cout << "row = " << i << " : ";
if (column < allData[i].size() )
{
cout << allData[i][column] << "\n";
}
else
{
cout << "column does not exist for this row\n";
}
cout << "\n";
}
-
May 10th, 2012, 08:50 AM
#5
Re: Reading file into array
 Originally Posted by Philip Nicoletti
Is this what you want ?
Code:
int column = 2; // 0-based (so 3rd column)
for (int i=0; i<allData.size(); ++i)
{
cout << "row = " << i << " : ";
if (column < allData[i].size() )
{
cout << allData[i][column] << "\n";
}
else
{
cout << "column does not exist for this row\n";
}
cout << "\n";
}
Thank Philip Nicoletti your help was very fruitful, i modified it for my purpose
thumb up to you
Regards,
Ewa
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|