这是一个例子(不要介意逗号):
FILE1.TXT:
1 text1, 7 text2, 4 text3
FILE2.TXT:
3 text4, 8 text5, 2 text6
所以文件3应为:
1 text1, 2 text6, 3 text4, 4 text3, 7 text2, 8 text5
到目前为止,我已经像我说的那样创建了2个二进制文件,这些文件中有一个数字和文本。我还有点把文件内容写成数组。问题是 - 我不知道如何对它们进行排序并将它们存档。
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
class faili {
int atslega;
char * komponente;
public:
faili(int x, char * s) {
komponente = new char[20];
strcpy(komponente, s);
atslega = x;
}
faili() {
komponente = new char[20];
atslega = 0;
}
void writeToFile(fstream & f) {
f.write((char *)&atslega, sizeof(int));
f.write(komponente, 20);
}
void readFromFile(fstream & f) {
f.read((char *)&atslega, sizeof(int));
f.read(komponente, 20);
}
int getKey() {
return atslega;
}
void printIt() {
cout << atslega << " " << komponente << endl;
}
};
typedef vector<faili *> failiMas;
void printMas(failiMas arr) {
for (failiMas::iterator it = arr.begin(); it != arr.end(); it++) {
(*it)->printIt();
}
};
class salidzinat {
public:
bool operator()(faili * i1, faili * i2) {
return i1->getKey() > i2->getKey();
}
};
typedef vector<faili *> failiMas;
int main() {
ofstream outputFile1w("f1.txt");
/*faili * i1 = new faili(1, "sad");
faili * i2 = new faili(3, "asdasd");
faili * i3 = new faili(6, "dfgdfc");
faili * i4 = new faili(4, "dija");
faili * i5 = new faili(8, "yjk");
faili * i6 = new faili(2, "ruifjer");
*/
faili ier1(1, "sad");
faili ier2(3, "asdasd");
faili ier3(6, "dfgdfn");
faili ier4(4, "dija");
faili ier5(8, "yjk");
faili ier6(2, "ruifjer");
fstream f1("f1.txt");
ier1.writeToFile(f1);
ier2.writeToFile(f1);
ier3.writeToFile(f1);
f1.close();
f1.open("f1.txt");
faili a, b, c;
a.readFromFile(f1);
b.readFromFile(f1);
c.readFromFile(f1);
cout << "Faila f1.txt saturs: \n";
a.printIt();
b.printIt();
c.printIt();
f1.close();
ofstream outputFile2w("f2.txt");
fstream f2("f2.txt");
ier4.writeToFile(f2);
ier5.writeToFile(f2);
ier6.writeToFile(f2);
f2.close();
f2.open("f2.txt");
faili d, e, f;
d.readFromFile(f2);
e.readFromFile(f2);
f.readFromFile(f2);
cout << "Faila f2.txt saturs: \n";
d.printIt();
e.printIt();
f.printIt();
f2.close();
ifstream inputFile1r;
inputFile1r.open("f1.txt", ios::in);
int count = 0;
string textarr[5];
string textsave = "Null";
while (!inputFile1r.eof()) {
inputFile1r >> textsave;
textarr[count] = textsave;
cout << textarr[count] << endl;
count++;
}
inputFile1r.close();
ifstream inputFile2r;
inputFile2r.open("f2.txt", ios::in);;
while (!inputFile2r.eof()) {
inputFile2r >> textsave;
textarr[count] = textsave;
cout << textarr[count] << endl;
count++;
}
system("Pause");
//-----------------------------------------------------------------------------------------------------------------------
};
我当然不需要你为我做作业:P。我有点想知道我是否正确思考,接下来应该做些什么,因为我很失落。
答案 0 :(得分:0)
您可以在循环内使用eof()条件并跟踪所有数字。如果出现的新数字大于“最大数”,则会在数组中向前移动。您可以使用许多排序技术,但这是最基本的排序技术。获得排序后的数组后,您只需将数据写入第三个文件即可。
#include <unistd.h>
int c;
int greatest = 0;
int arr = new int[N];
while ((c = getchar()) != EOF)
{
faili l;
string s = l.readFromFile(file);
int num = atoi(s);
#Compare with greatest and write in array
}