hey there. i'm trying to read from 2 sorted txt files and then merge them into 1 sorted txt file. but i can't execute them successfully even though the exe. window is out and then they gave me this error message, yet there is no error found when i compiled.
anyone out there kind enough to help me and point out my mistake? thank you so much!
The code might be different but the topic is the same!
Oh my mistake, it's not.
Sorry.
Anyway, then;
You could open the files with fstream, read them into a C string (char* - character pointer) array like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Stores each line in an element within a C-string
char* myArray1[someNumber];
std::fstream myFile;
myFile.open("myTextFile.txt");
if (myFile.is_open()) {
int i = 0;
while (!(myFile.eof())) {
getline(myFile, myArray1[i]);
++i;
} else {
std::cout << "Error opening file \"myTextFile.txt\" , check if file exists and can be accessed.\n";
}
myFile.close();
Once you've done that, write each array to the file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Writes each infile to an outfile (thus merging them):
ofstream myOutFile;
myOutFile.open("myMergedFile.txt");
if (myOutFile.is_open()) {
int i = 0;
while (!(myFile.eof())) {
myOutFile << myArray1[i];
++i;
}
} else {
std::cout << "Error writing file \"myMergedFile.txt\" , check if previous files could be read and current file can be accessed.\n";
}
myOutFile.close();