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

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.

Hi

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.

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 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.

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( line.find("heading") != string::npos)
{
   // heading found
}
else if( line.find("value") != string::npos)
{
   .// value found
}

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;
}

>>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" ?

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.