Problem with reading multiple text files and merging them into one output file

Hi, I need help to program as stated by topic.

Basically, I need to create a program that asks user to enter the number of input files, input filenames and output filename. Next, every line of the input files respectively are to be concatenated with a tab between each data. For example,

Inputs:
Your
My
Her

Dog
Cat
Wolf

Cute
Happy
Sad

Output:
Your "\t" Dog "\t" Cute
My "\t" Cat "\t" Happy
Her "\t" Wolf "\t" Sad

For 2 input files only, my code works fine:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
ifstream readFile ("in1.txt");
ifstream readFile2 ("in2.txt");
ofstream writeFile ("out.txt");
string x;

while (readFile >> x) {
writeFile << x << "\t";
if (!readFile2.eof()) {
getline(readFile2, x);
writeFile << x << endl;
}
}
writeFile << endl;
readFile.close();
readFile2.close();
writeFile.close();

return 0;
}

My current code on problem:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
int n, i;
string x, readFile[100], outfile;

cin >> n; //User input number of files.

cin >> outfile;

for (i = 0; i < n; i++) {
ifstream readFile[i];
getline(readFile[i], x);
cout << x << "\t";
if (readFile[n]) {
cout << endl;
}
}
return 0;
}

After this, I have no clue on how to continue but the code above has no compilation error.
What aspects of C++ are you allowed to use? Can you use std::vector? Do you have access to a compiler that support C++11 move semantics?
You could just continue your original idea of writing a loop that reads one line from each of the three files and immediately writes them all out..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    std::ifstream readFile ("in1.txt");
    std::ifstream readFile2("in2.txt");
    std::ifstream readFile3("in3.txt");
    std::ofstream writeFile("out.txt");

    std::string x, y, z;
    while (readFile >> x && readFile2 >> y && readFile3 >> z)
        writeFile << x << '\t' << y << '\t' << z << '\n';
}
Cubbi, the user decides how many input files there are, nobody mentioned 3 arbitrarily...?
Oh, right, nevermind, I looked at the sample inputs instead of the actual description.
L B, sorry for posting so late. Apparently, I have not been taught how to use std::vector and I'm using DevCpp 5.4.2
Do you have an upper limit for what the user is allowed to type in? E.g. is the user allowed to say "I have 1000 input files" and you have to accept it? Or can you tell the user "Nope, you get 50 and that's final."? Because the answer to this question makes a big difference.
It's generally safe to assume 100 is the max
Update to my current code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
int n;
string x, inputFilename[100], outputFilename;
const char *cstr[100];

cin >> n; //User inputs number of files to concatenate.

for (int i = 0; i < n; i++) {
cin >> inputFilename[i]; //Input the names of all the input files that user wants.
}
cin >> outputFilename;

for (int i = 0; i < n; i++) {
cstr[i] = inputFilename[i].c_str();
}

return 0;
}

For the pointer, I'm not sure whether it's useful because I only have some knowledge about const char working well for some functions.

As long as the program works for 5 to 10 files, I think it's good to go.
Last edited on
helpstudent wrote:
1
2
3
4
5
6
7
	const char *cstr[100];
	
//...

	for (int i = 0; i < n; i++) {
		cstr[i] = inputFilename[i].c_str();
	}
First, you don't need to do this. Second, you should never do this - .c_str() is only temporary and storing a pointer to it is no good. Just don't do it ;)

Also, I wasn't thinking properly when I asked about the max - first I should have asked if you are allowed to/know how to dynamically allocate arrays with a user-defined size. You can ask how many files first and then allocate the array when you know the size.
Topic archived. No new replies allowed.