Connecting Tech Pros Worldwide Help | Site Map

Mulltimap multiple file output

Newbie
 
Join Date: May 2009
Posts: 21
#1: 4 Weeks Ago
Hi.
I am trying to create multiple output files while reading a single input file. The multiple files will be created based on the value in the first field.

My set1 contains 1,2,3,2,1,3. That is Field1 values.
My multimap contains the other fields (field2 and field3).
Now what I am trying to do is to use ofstream to output multiple output files dynamically. Its just creating 1 file and duplicating records :(

Here is my input file:-
Expand|Select|Wrap|Line Numbers
  1. 1    AAAAAA    BBBBBB    
  2. 2    DDDDDD    EEEEEE
  3. 3    GGGGGG    HHHHHH
  4. 2    NNNNNN    LLLLLL
  5. 1    UUUUUU    KKKKKK  
  6. 3    SSSSSS  BBBBBB
This is how I expect my output to be:-
Expand|Select|Wrap|Line Numbers
  1. file1.txt:
  2. 1    AAAAAA    BBBBBB
  3. 1    UUUUUU    KKKKKK
  4.  
  5. file2.txt:
  6. 2    NNNNNN    LLLLLL
  7. 2    DDDDDD    EEEEEE
  8.  
  9. file3.txt:
  10. 3    GGGGGG    HHHHHH
  11. 3    SSSSSS  BBBBBB
Here is my code so far:-
Expand|Select|Wrap|Line Numbers
  1.           set1.insert(field1);
  2.      for(itv=set1.begin(); itv !=set1.end(); itv++)
  3.  
  4.      {
  5.        if(chr.compare(*itv)==0){
  6.           string filenamei = "myfile";
  7.           multimap<string,string> mmi;
  8.           ofstream out;
  9.           mmi.insert (pair<string,string>(band,start));
  10.           out.open(filenamei.c_str());
  11.        for (it= mmi.begin(); it != mmi.end(); it++){
  12.  
  13.               cout << (*it).first << "\t" << (*it).second << endl;
  14.        }
  15.  
  16.        }//end of if loop
  17.      }
Thanks
Familiar Sight
 
Join Date: Apr 2007
Posts: 188
#2: 4 Weeks Ago

re: Mulltimap multiple file output


Your code is only opening one output file.

If you want to write to multiple files you need to have multiple output streams (one for each file), and then write the data to each one.

I suppose you can do it with one output stream just open file1, write data, close stream, open file2, write data, close, etc...
Newbie
 
Join Date: May 2009
Posts: 21
#3: 4 Weeks Ago

re: Mulltimap multiple file output


Ya thats true but as per my problem there could be any number of output files that could be generated based on my first field values. So I was thinking a way by what I can do it dynamically ?
Like using a for loop and creating output files

Like file[i].txt

Thanks
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,148
#4: 4 Weeks Ago

re: Mulltimap multiple file output


Your code already contains a for loop editing the file, all you have to do is use a different file name each time round the loop.
Reply