У меня есть два текстовых файла, каждый с неизвестным числом целых чисел, отсортированных от самого низкого до самого высокого ... например:Запись отсортированных чисел из функции в текстовый файл?
входного файла 1: 1 3 5 7 9 11 ...
входной файл 2 : 2 4 6 8 10 ....
Я хочу взять эти цифры из обоих файлов, сортировать от низкого до высокого и затем выводить полный список отсортированных номеров из обоих входных файлов в один выходной файл , То, что я до сих пор ...
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "iosort.h"
int main()
{
const char* filename1 = "numberlist1.txt";
const char* filename2 = "numberlist2.txt";
std::ofstream ofs("output.txt");
std::ifstream ifs1, ifs2;
std::string input1, input2;
ifs1.open(filename1);
std::getline(ifs1, input1);
std::cout << "Contents of file 1: " << input1 << std::endl;
ifs2.open(filename2);
std::getline(ifs2, input2);
std::cout << "Contents of file 2: " << input2 << std::endl;
ioSort(ifs1, ifs2, ofs);
return 0;
}
и моя функция ...
#include <fstream>
#include <sstream>
#include <vector>
#include "iosort.h"
void ioSort(std::ifstream& in1, std::ifstream& in2, std::ofstream& out)
{
int a, b;
std::vector<int> f1, f2, f3; //create one vector for each input stream
while (in1 >> a)
{
f1.push_back(a);
}
while (in2 >> b)
{
f2.push_back(b);
}
//now f1 and f2 are vectors that have the numbers from the input files
//we know that in these input files numbers are sorted from low to high
if (f1.size() > f2.size()) //input stream 1 was larger
{
for (int i = 0; i < f2.size(); i++)
{
if (f1[i] > f2[i]) //number at input vector 2 less that respective pos
{ //in input vector 1
f3.push_back(f2[i]);
}
else if(f1[i] == f2[i]) //numbers are equal
{
f3.push_back(f1[i]);
f3.push_back(f2[i]);
}
else //number in 1 is less than that in vector 2
{
f3.push_back(f1[i]);
}
}
for (int i = f2.size(); i < f1.size(); i++)
{
f3.push_back(f1[i]); //push remaining numbers from stream 1 into vector
}
}
else //input stream 2 was larger
{
for (int i = 0; i < f1.size(); i++)
{
if (f1[i] > f2[i]) //number at input vector 2 less that respective pos
{ //in input vector 1
f3.push_back(f2[i]);
}
else if(f1[i] == f2[i]) //numbers are equal
{
f3.push_back(f1[i]);
f3.push_back(f2[i]);
}
else //number in 1 is less than that in vector 2
{
f3.push_back(f1[i]);
}
}
for (int i = f1.size(); i < f2.size(); i++)
{
f3.push_back(f1[i]); //push remaining numbers from stream 2 into vector
}
}
//send vector contents to output file
for (int i = 0; i < f3.size(); i++)
{
out << f3[i] << " ";
}
}
Everytime я скомпилировать и запустить файл output.txt создается, но он пуст. Может кто-нибудь указать мне, что я делаю неправильно. Если, в основном, я делаю что-то вроде:
out << 8 << " " << 9 << std::endl;
затем он будет отображаться в выходном файле.
что делает отладчик говорят, что происходит в вашей фазе слияния – pm100
я закрывал OFS по возвращении из iosort - но то, вероятно, не является причиной, и положить Endl на него в iosort – pm100
Put все числа из всех файлов в одном файле [std :: set] (http://en.cppreference.com/w/cpp/container/set) или [std :: multiset] (http: //en.cppreference. ком/ж/CPP/контейнер/мультимножеством). Они будут автоматически отсортированы для вас. – crayzeewulf