473,809 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Mulltimap multiple file output

21 New Member
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
Oct 22 '09 #1
3 3826
mac11
256 Contributor
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...
Oct 22 '09 #2
crochunter
21 New Member
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
Oct 22 '09 #3
Banfa
9,065 Recognized Expert Moderator Expert
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.
Oct 23 '09 #4

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

Similar topics

11
17842
by: Georg Teichtmeister | last post by:
Hello! We are developing a math - library for realtime applications and want to use some given mathlibraries as base(ipp, MTL, .. ). Our library is a wrapper for those and you should be able to select the underlying library by template parameter. Therefore we split up our library in two namespaces: The first one defines namespace-global functions which call the baselibrary functions. These are templatefunctions which some
4
1980
by: Tristan Miller | last post by:
Greetings. I would like to produce a static multilingual website in XHTML. Is it possible to specify each web page in its own XML file, but have all of the translations encapsulated in that one file, and then process each XML file to generate separate language-variant XHTML files? For example, say we have a file foo.xml which contains, in part, something like the following:
5
5406
by: Jeff | last post by:
We are using .Net and the wsdl Utility to generate proxies to consume web services built using the BEA toolset. The data architects on the BEA side create XML schemas with various entities in separate files for ease of maintainability. These schemas are all part of the same namespace. When defining a web service that access more than one of these entities, the wsdl file generated by BEA contains multiple schema elements with the same...
13
2489
by: a.zeevi | last post by:
free() multiple allocation error in C ==================================== Hi! I have written a program in C on PC with Windows 2000 in a Visual C environment. I have an error in freeing multiple allocation as follows: 1. I allocated an array of pointer. 2. I Read line by line from a text file. 3. I allocated memory for the read line.
4
2782
by: yancheng.cheok | last post by:
Recently, I try to replace #define with const in header file. However, there are concerns on, multiple const object will be created, if the header file is included in multiple cpp files. For example: In version.h ------------ #ifndef VERSION #define VERSION
15
15079
by: leorulez | last post by:
Is there any way to read multiple files (more than 1000 files) and then write into one single output file using C? Right now in my program, I have a loop which asks for the filename and writes into the output file but this is tedious. Imagine typing 1000 filenames...is there a efficient way to do this?? Thanks
3
11696
by: jparulan | last post by:
Hi All, I'm using SOAP3.0. I was able to successfully call a WSDL file and get a value properly. But when the WSDL changed to have a MULTIPLE <element name> it was failing. This code works BEFORE: strCurrency = oSOAP.eServeSearch(CSTR(Request.Form("T1"))) until the WSDL file changed to have more <element name> T1 is just a field that accepts a STRING
3
8625
by: Tim | last post by:
Hi Folks, I'm used to a UNLOAD command that allows me to dump to a named flat file the results of any SELECT statement. Hence one can build a single SQL file which contains multiple SQL statements each of which 'unloads' to its own unique file. unload to 'file1.txt' select * from ...... unload to 'file2.txt' select * from ...... and so on....
3
3930
by: nigelesquire | last post by:
Please help! I'm trying to clone and delete multiple rows with JavaScript. I need two delete buttons that work...! I only have one for now, but it's not working properly, the output count is messing up. Problems:
0
10639
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10120
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7661
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5688
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.