By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,604 Members | 1,444 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,604 IT Pros & Developers. It's quick & easy.

trying to create aan array from a text file

P: 1
Hi

I am trying to build an array out of an input text file as shown below.

pattern1
SAF 10 1 10
CLB 20 18 10 11

pattern2
SAF 20 1 10 11
SAF 30 1 10
SAF 40 1 11
SAF 10 0 10
SAF 11 0 11

pattern3
SAF 31 1 10
SAF 21 1 11
SAF 10 0 10
SAF 11 0 11

pattern4
SAF 10 1 10
CLB 20 18 10
SAF 21 1 11
SAF 11 0 11

now here the first 3 fields in each line constitute the value field (like value1).Now suppose under heading one there is some value for value1 then it needs to go in the first column. Same way if heading4 has a value for value1 it needs to go under corresponding column4. So the output array should generated would be something like this

[CLB 20 18] [10 11] [ NULL] [ NULL] [ 10]
[SAF 10 0] [ NULL ] [ 10 ] [10 ] [NULL ]
[SAF 10 1 ] [10 ] [NULL ] [ NULL ] [10 ]
[SAF 11 0 ] [NULL ] [11 ] [11] [ 11 ]
[SAF 20 1 ] [NULL ] [10 11 ] [NULL ] [NULL ]
[SAF 21 1 ] [NULL ] [NULL ] [11 ] [11 ]
[SAF 30 1 ] [NULL ] [10 ] [ NULL ] [ NULL ]
[ SAF 31 1 ] [NULL ] [ NULL ] [10 ] [NULL ]
[ SAF 40 1 ] [NULL ] [11 ] [ NULL] [ NULL ]

The square brackets are just to show the whole thing as one string

Now if I read the complete line using getline then I am not able to ignore the lines with word pattern. If I use cin then I get each word individually. I hope I am not making it too complicated. My code so far is:

#include<iostream>
#include<string>
#include <fstream>
using namespace std;

int main(){
ifstream input("detection_report.txt", ios::in);
int columns = 0;

if(!input)
{
cerr<<"File could not be opend\n";
exit(1);
}

string value;
while(getline(input,value)){

string check = value.substr(0,1);
if (check == "p")
{
columns = columns + 1;
}
else{

//Here I want to read the lines without the "pattern" words.
//Secondly I just want to read only the first three groups to put in an array.
//which is quite difficult using a substr as some will need 8 characters and some will need 9.
}
cout << value << "\n";
}

input.close();
cout << "Columns: " << columns <<"\n";

return 0;
}
Nov 16 '06 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.