nik23
Гость
|
|
« : Июнь 30, 2015, 12:41 » |
|
Здравствуйте! Помогите,пожалуйста, решить проблему! Написал алгоритм внешней сортировки прямым слиянием. Почему то, если использовать стандартный сишные функции работы с файлами он работает, но при использовании QFile нет. Вот код: #include <QFile> #include <QTextStream> #include <QMessageBox> void straightSort (){ int a1, a2, k, i, j, n; //FILE *mainFile, *subFile1, *subFile2; QFile mainFile("file.csv"), subFile1("subFile1.csv"), subFile2("subFile2.csv");
n = 0; // mainFile = fopen(name,"r"); mainFile.open(QIODevice::ReadOnly); // while ( !feof(mainFile) ) { while (!mainFile.atEnd()) { //fscanf(mainFile,"%d",&a1); mainFile.readLine(); n++; } //fclose(mainFile); mainFile.close(); k = 1; while ( k < n ){ //mainFile = fopen(name,"r"); mainFile.open(QIODevice::ReadOnly); //subFile1 = fopen("smsort_1","w"); subFile1.open(QIODevice::WriteOnly); //subFile2 = fopen("smsort_2","w"); subFile2.open(QIODevice::WriteOnly); //if ( !feof(mainFile) ) fscanf(mainFile,"%d",&a1); if (!mainFile.atEnd()){ QString str = mainFile.readLine(); a1 = str.toInt(); } QMessageBox msgBox; msgBox.setText(QString::number(a1)); msgBox.exec();
//while ( !feof(mainFile) ){ while (!mainFile.atEnd()){ // for ( i = 0; i < k && !feof(mainFile) ; i++ ){ for ( i = 0; i < k && !mainFile.atEnd() ; i++ ){ //fprintf(subFile1,"%d ",a1); subFile1.write(QString::number(a1).toUtf8()); subFile1.write("\n"); //fscanf(mainFile,"%d",&a1); QString str = mainFile.readLine(); a1 = str.toInt(); } // for ( j = 0; j < k && !feof(mainFile) ; j++ ){ for ( j = 0; j < k && !mainFile.atEnd() ; j++ ){ //fprintf(subFile2,"%d ",a1); subFile2.write(QString::number(a1).toUtf8()); subFile2.write("\n"); //fscanf(mainFile,"%d",&a1); QString str = mainFile.readLine(); a1 = str.toInt(); } } //fclose(subFile2); subFile2.close(); // fclose(subFile1); subFile1.close(); //fclose(mainFile); mainFile.close();
//mainFile = fopen(name,"w"); mainFile.open(QIODevice::WriteOnly); //subFile1 = fopen("smsort_1","r"); subFile1.open(QIODevice::ReadOnly); //subFile2 = fopen("smsort_2","r"); subFile2.open(QIODevice::ReadOnly);
//if ( !feof(subFile1) ) fscanf(subFile1,"%d",&a1); if (!subFile1.atEnd()){ QString str = subFile1.readLine(); a1 = str.toInt(); }
//if ( !feof(subFile2) ) fscanf(subFile2,"%d",&a2); if (!subFile2.atEnd()){ QString str = subFile2.readLine(); a2 = str.toInt(); }
// while ( !feof(subFile1) && !feof(subFile2) ){ while (!subFile1.atEnd() && !subFile2.atEnd()) { i = 0; j = 0; //while ( i < k && j < k && !feof(subFile1) && !feof(subFile2) ) { while ( i < k && j < k && !subFile1.atEnd() && !subFile2.atEnd()) {
if ( a1 < a2 ) { //fprintf(mainFile,"%d ",a1); mainFile.write(QString::number(a1).toUtf8()); mainFile.write("\n"); //fscanf(subFile1,"%d",&a1); QString str = subFile1.readLine(); a1 = str.toInt(); i++; } else { //fprintf(mainFile,"%d ",a2); mainFile.write(QString::number(a2).toUtf8()); mainFile.write("\n"); //fscanf(subFile2,"%d",&a2); QString str = subFile2.readLine(); a2 = str.toInt(); j++; } } //while ( i < k && !feof(subFile1) ) { while ( i < k && !subFile1.atEnd() ) { //fprintf(mainFile,"%d ",a1); mainFile.write(QString::number(a1).toUtf8()); mainFile.write("\n"); //fscanf(subFile1,"%d",&a1); QString str = subFile1.readLine(); a1 = str.toInt(); i++; } // while ( j < k && !feof(subFile2) ) { while ( j < k && !subFile2.atEnd() ) { // fprintf(mainFile,"%d ",a2); mainFile.write(QString::number(a2).toUtf8()); mainFile.write("\n"); //fscanf(subFile2,"%d",&a2); QString str = subFile2.readLine(); a2 = str.toInt(); j++; } } //while ( !feof(subFile1) ) { while (!subFile1.atEnd()) { // fprintf(mainFile,"%d ",a1); mainFile.write(QString::number(a1).toUtf8()); mainFile.write("\n"); //fscanf(subFile1,"%d",&a1); QString str = subFile1.readLine(); a1 = str.toInt(); } //while ( !feof(subFile2) ) { while (!subFile2.atEnd()) { //fprintf(mainFile,"%d ",a2); mainFile.write(QString::number(a2).toUtf8()); mainFile.write("\n"); //fscanf(subFile2,"%d",&a2); QString str = subFile2.readLine(); a2 = str.toInt(); } subFile2.close(); subFile1.close(); mainFile.close(); k *= 2; } //subFile1.remove(); //subFile2.remove(); }
|