-1

Task is - For given ordered numerical files f and g, write a program to merge these two files into an ordered file ff . But my while loop is don't work correctly and don't comletely understand how to read data from the files by the end

#include <iostream>
#include <fstream>

using namespace std;

int main() {
    ofstream ff;
    ifstream f, g;
    const char* n1 = "f.txt", * n2 = "g.txt", * n3 = "ff.txt";
    f.open(n1);
    g.open(n2);
    ff.open(n3);
    if (!f.is_open() || !g.is_open()) {
        cout << "File problems " << endl;
        return 1;
    }
    char strf[5], strg[5];
    int f_, g_;
    f >> strf;
    g >> strg;
    f_ = atoi(strf);
    g_ = atoi(strg);
    while (!f.eof() && !g.eof()) {
        while (f_ <= g_) {
            if (f_ != g_) {
                ff << f_;
            }
            f >> strf;
            f_ = atoi(strf);
        }
        g >> strg;
        g_ = atoi(strg);
        ff << g_;
    }
    ff.close();
    return 0;
}
  • 2
    You made the task harder for us (and future you) by using very poor variable naming. – drescherjm Jan 23 '23 at 18:53
  • FYI, a standard coding style is to declare one variable per line. This technique helps with pointer declarations. Also, use a named constant instead of the number 5. – Thomas Matthews Jan 23 '23 at 18:54
  • 2
    `while (!f.eof() && !g.eof()) {` is likely wrong: [https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – drescherjm Jan 23 '23 at 18:54
  • My advice to you is to obtain a debugger and step through the code 1 line at a time looking at the variables and flow after each line is executed so you can determine where your code's behavior deviates from your expectations. – drescherjm Jan 23 '23 at 18:55
  • you should write a function `void foo(ostream& out, istream& in1, istream& in2)`, test with stringsstream fed from hard coded strings, once it works, use files. Do one thing at a time, currently you need to debug reading from file, the calculations and writign to file. Much simpler to deubg them seperately – 463035818_is_not_an_ai Jan 23 '23 at 18:56
  • I don't understand. Why are you inputting as string, then converting to numbers; when you can input as numbers? – Thomas Matthews Jan 23 '23 at 19:05

1 Answers1

-1

t looks like your while loop is not working correctly because it is not properly handling the cases where one file reaches the end of its contents before the other. Additionally, you are not handling the case where both files have the same value at the current position.

Here is one way you could modify your code to correctly merge the two files:

> while (!f.eof() && !g.eof()) {
    if (f_ <= g_) {
        ff << f_ << ' ';
        f >> strf;
        if (!f.eof()) {
            f_ = atoi(strf);
        }
    } else {
        ff << g_ << ' ';
        g >> strg;
        if (!g.eof()) {
            g_ = atoi(strg);
        }
    }
}
while (!f.eof()) {
    ff << f_ << ' ';
    f >> strf;
    if (!f.eof()) {
        f_ = atoi(strf);
    }
}
while (!g.eof()) {
    ff << g_ << ' ';
    g >> strg;
    if (!g.eof()) {
        g_ = atoi(strg);
    }
}

This code first compares the current value from each file and writes the smaller one to the output file. It then reads the next value from the file that was just written to the output file. If one file reaches the end of its conte

nts before the other, the code continues to read and write values from the remaining file until it also reaches the end.

Also, in the current code you are reading input as string but storing it as int after converting from string using atoi(). But this will be problem when you have negative numbers. In that case you can use istringstream or other library to read the value as integer.