Web_Sailor 0 Junior Poster

Hi gurus,

I have a simple question regarding file parsing. I am trying to parse a file and store the fields I require in a multimap. Now I want to pass an array which is of int type and specifies field number to be extracted in a file. I am able to do it to some extent but here I am able to pass only the first value of an array correctly which specifies a field number.

To simplify things here is what i am doing:

suppose my input file:-

aaa	bbb	ccc	ddd	111	222	333
eee	fffff	gg	hhh	444	555	666
iiiiiii	jjjjj	kk	llllllll	777	888	999

Now my 5 strings field1, field2, field3, field4, field5 are quite straight forward which will take the first 5 fields of input file and store them into the multimap as following:-

aaa	bbb	ccc	ddd	111
eee	fffff	gg	hhh	444
iiiiiii	jjjjj	kk	llllllll	777

Now the most important thing comes in the addfields string. Here I want to store fields based on the input fields given in the array

arr[]

which is a function argument. So as my int values in main for

arr[]

are

arr[] = {6,7}

so I want my addfields string to contain both the fields and store in a multimap.

What I mean is 6th and 7th field should be added to addfields string so that it can have values

222	333
555	666
888	999

which i can pass to multimap but now as per my current code as below i am only able to pass the below field values to addfields string.

222
555
888

Here is my full file parsing code below:-

#include<iostream>
#include<fstream>
#include<sstream>
#include<cctype>
#include<vector>
#include<string>
#include<list>
#include<set>
#include<map>

using namespace std;

  bool maxlengthfirst(pair<string,string> i,pair<string,string> j){ 
              return i.first.size()<j.first.size();
  }

  bool maxlengthsecond(pair<string,string> i,pair<string,string> j){ 
              return i.second.size()<j.second.size();
  }

void fixer(const char *inputfilename, int arr[]){

     multimap<string, string> mm;
     multimap<string,string>::iterator it;
     set<string> set1;
     set<string>::iterator itv;

     string field1, field2, field3, field4, group, combVal, addfields, field5;
     int arraySize = sizeof(arr)/sizeof(arr[0]);

         FILE *in;
         char line[1000];
         char *token;
         in = fopen(inputfilename,"rt+");
         if( in == NULL) exit(1);

  while(! feof(in)) {
            fgets(line, 1000, in);
     if (! feof(in))
     {
            int count=0;
            token = strtok(line, "\t\n");

         while (token != NULL)
         {
           if(++count){   
             if(count==6){
                   field1.assign(token);
             } 
             if(count==7){
                   field2.assign(token);
             }
             else if(count==2){
                   field3.assign(token);
             }
             else if(count==3){
                  field4.assign(token);
             }  
             else if(count==9){
                   field5.assign(token);
             }    
    for(int n=0; n<len; n++){
             if(count==arr[n]){
            //cout << arr[n] << endl;
                 addfields.assign(token);

             }     
    }                  
          } // end of main token if loop
                   token = strtok(NULL, "\t\n");
        }   // end of inner while loop
                        
       combVal = field2+"\t"+field3+"\t"+field4+"\t"+field5+"\t"+addfields;  
       group = field1+"\t";
       

                    mm.insert(pair<string, string>(group, combVal));
               
        } // end of main if loop after while loop

 }// end of main while loop
  
         int firstSize=max_element(mm.begin(), mm.end(),maxlengthfirst)->first.size();  
         int secondSize=max_element(mm.begin(), mm.end(),maxlengthsecond)->second.size(); 
          for (it=(mm.begin()); it!=(mm.end()); it++)
          {
               cout.width(firstSize);
               cout.fill('0');              
               cout << it->first <<"\t";
               cout.width(secondSize);
               cout.fill(' ');
               cout << it->second << endl;
          } 
  mm.clear();
}

// Main Method starts 
int main(int argc, char* argv[]) 
{
          string filename = argv[1];
          const char* inputfilename = filename.c_str();
          int arr[] = {6,7};
          fixer(inputfilename, arr);
          return 0;
}
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.