Hi,
I was wondering if there is any way to get the largest size of a multimap key / value. I mean something like this:-
if there are 4 values in a multimap:-
my
name
is
web_sailor
Suppose I want to get the maximum size key / value. Like here I should get the size of "web_sailor" 10 which is the maximum size and 2 is the minimum size for in / my. This is because I want to set the record lengths as fixed with printf function as per my requirement.
One more question:-
suppose I have a file like below and I would like to create 3 output files based on my first field .. .i.e, 1 /2/3.
Thats means 3 lines will go to the three files
1 AAAAAA BBBBBB CCCCC
2 DDDDDD EEEEEE FFFFF
3 GGGGGG HHHHHH IIIII
2 copy paste insert
1 andrew mathew horto
I expect something like this to happen
file1:-
1 AAAAAA BBBBBB CCCCC
1 andrew mathew horto
file2:-
2 DDDDDD EEEEEE FFFFF
2 copy paste insert
file3:-
3 GGGGGG HHHHHH IIIII
Here is my code so far but I am not able to create file dynamically. I mean here I am predefining the file names.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void parseFile(string file, string line)
{
ofstream out(file.c_str(),ios::app);
out<<line<<endl;
out.close();
}
int main()
{
ifstream in("inputfile.txt");
string line = "";
int count = 0;
while(getline(in,line))
{
string file = "file";
count++;
switch (count)
{
case 1:
parseFile("file1.txt",line); break;
case 2:
parseFile("file2.txt",line); break;
case 3:
parseFile("file3.txt",line); break;
case 4:
parseFile("file2.txt",line); break;
case 5:
parseFile("file1.txt",line); break;
}
}
cin.ignore();
cin.get();
return 0;
}
Isn't there any way to automate this process ? I mean here I am defining the filenames in switch statement. i mean something like this :-
ofstream files[3];
for (int i = 0; i < 3; i++) {
string name = "file";
name += '0' + i + 1;
name += ".txt";
cout << name << endl;
files[i].open(name.c_str());
}
but here I don't know howto make a character string from an integer number in C++
for C it would be char line[100]; sprintf(line, "%d", i); // Googling :)
Need help in this with some example snippets.
Thanks