Concatenate strings across multiple files

I've danced on the brink of insanity trying to find a solution to this problem. I'm supposed to write a program that takes multiple input files, and concatenate them into a single output. For example, if I have 3 files that look like

(1.txt)
1
2
3

(2.txt)
Ann
Bob
Chris

(3.txt)
Female
Male
Male

then the output should be

(output.txt)
1 Ann Female
2 Bob Male
3 Chris Male

The toughest part here is the number of input files is given by user (, as are the input filenames) , so I couldn't do something like

1
2
3
ifstream fin1, fin2, fin3;
while((getline(fin1,fileOne) && getline(fin2,fileTwo)) && getline(fin3,fileThree))
fout << fileOne << " " << fileTwo << " " << fileThree << "\n";


I'm thinking of doing the following, but don't know how to implement another loop to check for end of file. Also, there's a trailing space in every line in the output that I have to get rid of if I am to go with this method.

1
2
3
4
5
6
7
8
9
for (i = 1, i < noOfInputs, i++){
//read first line of file i and write to output file and append a space character
}

for (i = 1, i < noOfInputs, i++){
//read second line of file i and write to output file and append a space character
}

...


Any help or comment is appreciated, thanks!
Start by Reading one file at a time into a container (array, vector, w/e). That will give you 3 arrays, for example, called num, name, and sex. Then just combine them in the output. More or less like:
1
2
3
4
5
6
for(int i = 0; i < size; ++i)
{
    output num[i];
    output name[i];
    output sex[i];
}
@admkrk That is still quite hardcoded to three "columns".

@drawar
Make vector<pair<ifstream,string>>.
Reserve room for each file.
Add open ifstreams to the vector.

Then use bool read( vector<pair<ifstream,string>> & ) as your while condition.
That function should do the getlines.
The body of while will then copy strings from vector to fout.


PS. GNU coreutils has command-line program paste that does this same operation. Open source, but probably C.
Thank you all for your helpful replies.
Actually I was looking for a more rudimentary method to tackle this problem, i.e. one that does not require the use of vector and what not. I'm building my code as per admkrk's instruction and will post back here if anything comes up.
Last edited on

The toughest part here is the number of input files is given by user (, as are the input filenames) , so I couldn't do something like

Why not? Did you try it? But remember you need to open all of the files, your first snippet doesn't actually open any files.

Try to avoid reading the files in their entirety. If the files are large you might run out of memory. You should be able to open all the files and then process them one line at a time with something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool done=false;
string str, output;
while (!done) {
    output.erase();
    for (int i = 0 i < files.size() ++i) {
       if (!(files[i] >> str)) {
          done = true;
          break;
       }
       if (i > 0) {
          output += ' ';
       }
       output += str;
    }
    if (done) break;
    cout << output;
}


Some details: this output lines until you hit the end of any of the input files.
Actually I was looking for a more rudimentary method to tackle this problem, i.e. one that does not require the use of vector and what not.

The use of standard library containers is the simple/basic method. admkrk's, dhayden's and my suggestions all use container and what not.
Topic archived. No new replies allowed.