简体   繁体   中英

C++ compile error on std::basic_ifstream

I'm supposed to write a program that merges the numbers in two files and writes all the numbers into a third file. The program takes input from two different files and writes its output to a third file. Each input file contains a list of numbers of type int in sorted order from the smallest to the largest. After the program is run, the output file will contain all the numbers in the two input files in one longer list in sorted order from smallest to largest. I'm not 100% sure my logic is correct. Thank you for your help.

inputFile1:

1 2 3 4 5 6 7 8 9 10

inputFile2:

11 12 13 14 15 16 17 18 19 20

#include <iostream>
#include <conio.h>
#include <fstream>

using namespace std;

int main()
{
int num1, num2;

ifstream inputFile;
ifstream inputFile2;
inputFile.open ("input1.txt");
inputFile2.open("input2.txt");
ofstream outputFile;
outputFile.open("output.txt");

inputFile >> num1;
inputFile2 >> num2;
while(inputFile.eof() && inputFile2.eof())
{
    if (num1 < num2)
    {
        outputFile << num1;
        inputFile >> num1;
    }
    else
    {       
        outputFile << num2;
        inputFile2 >> num2;
    }

}

inputFile.close();
inputFile2.close();
outputFile.close();

return 0;
}

If using the standard library is OK, then you can use merge :

int main()
{
    ifstream inputFile("input1.txt");
    ifstream inputFile2("input2.txt");
    ofstream outputFile("output.txt");

    typedef istream_iterator<int> IT;
    typedef ostream_iterator<int> OT;
    std::merge(IT(inputFile), IT(), IT(inputFile2), IT(), OT(outputFile, " "));

    outputFile.flush();
}

Also note that the i/ofstream constructor is capable of opening file during initialization.

inputFile2.open();

应该

inputFile2.close();

inputFile2.open(); is clearly a mistype for inputFile2.close();

But I'm afraid your logic is completely wrong. You haven't understood the point of the exercise. You are supposed to open both input files and the output file at the same time and you are not supposed to use an array to store and sort the numbers. If you do this right you will not need an array and will not need to do any sorting at all. That's the point of the exercise.

And to repeat what has already been said at least a million times on this forum. Do not use

while (!inputFile.eof())

It is incorrect. Do use

while (inputFile >> num)

This is a very basic sketch of how to do the merging. There's plenty of detail to fill in.

inFile1 >> num1;
inFile2 >> num2;
while (something or other)
{
  if (num1 < num2)
  {
    outFile << num1;
    inFile1 >> num1;
  }
  else
  {
    outFile << num2;
    inFile2 >> num2;
  }
}

There's an irony here, 'something or other' probably does involve using eof() . This is one occasion when using inFile.eof() is the right thing to do.

BTW the way your input data is not very good. Each file individually should be sorted but you shouldn't have all the numbers in file1 less than all the numbers in file2. That's not required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM