| | |
Trying to creating an array from a text file
![]() |
•
•
Join Date: Nov 2006
Posts: 4
Reputation:
Solved Threads: 0
Hi
I have a text file which has 4 heading which will constitute the number of columns and based on values an array has to be populated. To give a clear picture it is as:
input text file looks like:
heading1
value1 10
value2 10 11
heading2
value3 10 11
value4 10
value5 11
value6 10
value7 11
heading3
value8 10
value9 11
value6 10
value7 11
heading4
value1 10
value2 10
value9 11
value7 11
and based on the above the output array will look like as
Heading1 Heading2 Heading3 Heading4
value1 10 NULL NULL 10
value2 1011 NULL NULL 10
value3 NULL 1011 NULL NULL
value4 NULL 10 NULL NULL
value5 NULL 11 NULL NULL
value6 NULL 10 10 NULL
value7 NULL 11 11 11
value8 NULL NULL 10 NULL
value9 NULL NULL 11 11
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.
1. Now I am reading the pattern heading and then put the number of columns for the dynamic array.
2. Then I read all the lines and get the number of rows.
3. I read each line but I don't want to read the heading line (I want to ignore that) and then I do a substr and get an array for the unique names of value thing.
But unable to proceed from there...please guide and suggest the logic that can be used.
Thanks
I have a text file which has 4 heading which will constitute the number of columns and based on values an array has to be populated. To give a clear picture it is as:
input text file looks like:
heading1
value1 10
value2 10 11
heading2
value3 10 11
value4 10
value5 11
value6 10
value7 11
heading3
value8 10
value9 11
value6 10
value7 11
heading4
value1 10
value2 10
value9 11
value7 11
and based on the above the output array will look like as
Heading1 Heading2 Heading3 Heading4
value1 10 NULL NULL 10
value2 1011 NULL NULL 10
value3 NULL 1011 NULL NULL
value4 NULL 10 NULL NULL
value5 NULL 11 NULL NULL
value6 NULL 10 10 NULL
value7 NULL 11 11 11
value8 NULL NULL 10 NULL
value9 NULL NULL 11 11
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.
1. Now I am reading the pattern heading and then put the number of columns for the dynamic array.
2. Then I read all the lines and get the number of rows.
3. I read each line but I don't want to read the heading line (I want to ignore that) and then I do a substr and get an array for the unique names of value thing.
But unable to proceed from there...please guide and suggest the logic that can be used.
Thanks
Last edited by WolfPack; Nov 15th, 2006 at 9:03 pm. Reason: Removed unnecessary links from the post.
there are a couple ways to approach the problem.
(1) read the file twice. The first time through determine the number of columns in the 2d array from the "heading" line and the number of rows from the "value" line. Just keep track of the largest row and the largest column. Then rewind the file pointer to that the file can be reread from the beginning and allocate memory for the array, initialize all elements of the array to 0. The second time through the file get the column number from the "heading" line, the row number from the "value" string, then the value to be saved in array[row][col] by converting the remainder of the "value" string to integer.
(2) the second method is similary to the first except the file is only read once. Put each of the strings into an array so that this string array can be read instead of the file the second time through. If you are not comfortable with string arrays then the first method may be best for you.
Once you have the array it is a simple matter to print all its values.
And don't forget to deallocate all that memory before terminating the program.
(1) read the file twice. The first time through determine the number of columns in the 2d array from the "heading" line and the number of rows from the "value" line. Just keep track of the largest row and the largest column. Then rewind the file pointer to that the file can be reread from the beginning and allocate memory for the array, initialize all elements of the array to 0. The second time through the file get the column number from the "heading" line, the row number from the "value" string, then the value to be saved in array[row][col] by converting the remainder of the "value" string to integer.
(2) the second method is similary to the first except the file is only read once. Put each of the strings into an array so that this string array can be read instead of the file the second time through. If you are not comfortable with string arrays then the first method may be best for you.
Once you have the array it is a simple matter to print all its values.
And don't forget to deallocate all that memory before terminating the program.
Last edited by Ancient Dragon; Nov 15th, 2006 at 11:12 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
HI
So you know there are only 4 headings, right? Do you know how many values there are? If only 9 you can simply set up an integer array like
Then it's a simple matter to initialize the entire array with, say, -1 to represent NULL. Then when you read in each line you can assign the value read to the proper array location.
•
•
•
•
I have a text file which has 4 heading which will constitute the number of columns and based on values an array has to be populated.
int values[numheading][numvalues];Then it's a simple matter to initialize the entire array with, say, -1 to represent NULL. Then when you read in each line you can assign the value read to the proper array location.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Nov 2006
Posts: 4
Reputation:
Solved Threads: 0
Thats the main problem I am facing. I have to read as to how many times the heading thing is coming and based on that I have to decide the number of columns. Then I have to get the number of rows (based on total number of lines minus the heading lines.) Once this is done then read all the lines (may be in an array) and sort them out so that I get the 9 values. Now I am stuck up at the pattern matching thing for matching "heading". and the next time when I start reading the values I want to simply ignore the heading lines.
I already gave you an outline of how to write the program. Maybe you ignored it, or maybe you did not understand it?
if you use if - else if - statements it will be ok. Below will first check if "heading" is in the line, if not it will check if "value" is in the line.
if you use if - else if - statements it will be ok. Below will first check if "heading" is in the line, if not it will check if "value" is in the line.
C++ Syntax (Toggle Plain Text)
if( line.find("heading") != string::npos) { // heading found } else if( line.find("value") != string::npos) { .// value found }
Last edited by Ancient Dragon; Nov 16th, 2006 at 6:19 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Nov 2006
Posts: 4
Reputation:
Solved Threads: 0
Hi
Thanks for your reply. Actually the input is slightly different (sorry for interpreting the previous as same as this one)
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). So the output will be like
[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;
}

Thanks for your reply. Actually the input is slightly different (sorry for interpreting the previous as same as this one)
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). So the output will be like
[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;
}

Last edited by centos123; Nov 16th, 2006 at 6:48 pm.
>>I read the complete line using getline then I am not able to ignore the lines with word pattern.
Its not possible anyway, so what's the problem with that. If a line that contains the text "pattern" is read, then just ignore it and read another line. I already posted example of how to handle that, and I am not going to repeat what I already posted.
what is the difference between a line that starts with "SAF" and "CLB" ?
Its not possible anyway, so what's the problem with that. If a line that contains the text "pattern" is read, then just ignore it and read another line. I already posted example of how to handle that, and I am not going to repeat what I already posted.
what is the difference between a line that starts with "SAF" and "CLB" ?
Last edited by Ancient Dragon; Nov 16th, 2006 at 11:55 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Nov 2006
Posts: 4
Reputation:
Solved Threads: 0
Sorry for not being very clear but let me try to explain.
1. First I search for the pattern keyword. The number of times that keyword comes up, that I take as the number of columns.
2. Then I again read the file and store the three groups in each line for example (SAF 10 1) and (CLB 20 18) from each line and say I store them in an array.And in this process I have to ignore the lines with the word pattern.
3. Now I sort the array and remove duplicates so that I have 9 values as in the sample input. So by now I know that it has to be a 9X4 array.
4. Now again I read the lines in the file. This time all the entries under pattern 1 will go in column1 under their respective heading, and entries under pattern2 in column2 and so on.
5. So in each line the first 3 groups i.e. (SAF 10 1) or (CLB 20 18) can be assumed the names and the second group their values which needs to make up the array. For example under pattern1 when the line "SAF 10 1 10" is read. So under column1 for group (SAF 10 1) the value will be 10. Similary for (CLB 20 18 10 11) under column1 for group (CLB 20 18) the value will be
(10 11).
4. Same way when we read (SAF 10 1 10) under pattern4, the value 10 for group (SAF 10 1) goes under column4.
5. The rest of the fields have null values or whatever character desired.
I guess you have repeated the solution many times, but I could not get it. Anyway I really appreciate your help and the time .
Thanks
1. First I search for the pattern keyword. The number of times that keyword comes up, that I take as the number of columns.
2. Then I again read the file and store the three groups in each line for example (SAF 10 1) and (CLB 20 18) from each line and say I store them in an array.And in this process I have to ignore the lines with the word pattern.
3. Now I sort the array and remove duplicates so that I have 9 values as in the sample input. So by now I know that it has to be a 9X4 array.
4. Now again I read the lines in the file. This time all the entries under pattern 1 will go in column1 under their respective heading, and entries under pattern2 in column2 and so on.
5. So in each line the first 3 groups i.e. (SAF 10 1) or (CLB 20 18) can be assumed the names and the second group their values which needs to make up the array. For example under pattern1 when the line "SAF 10 1 10" is read. So under column1 for group (SAF 10 1) the value will be 10. Similary for (CLB 20 18 10 11) under column1 for group (CLB 20 18) the value will be
(10 11).
4. Same way when we read (SAF 10 1 10) under pattern4, the value 10 for group (SAF 10 1) goes under column4.
5. The rest of the fields have null values or whatever character desired.
I guess you have repeated the solution many times, but I could not get it. Anyway I really appreciate your help and the time .
Thanks
![]() |
Similar Threads
- Text File (Python)
- Help with a 2D array from a text file (C++)
- Homework: filling array from text file (VB.NET)
- Inputting text file data into an array, please help! (C++)
- Help Reading Info in Text File Into an Array (C++)
Other Threads in the C++ Forum
- Previous Thread: C-Strings : Bug in Code
- Next Thread: Query about mci
| Thread Tools | Search this Thread |
api array auto based beginner binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count delay-loading delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption equation error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node output parameter performance pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template test text text-file tivoli tree url variable vector video win32 windows winsock wordfrequency wxwidgets






