我需要为我的项目排序我的2个文本文件。其中一个文本文件是学生id,课程id和分数格式,另一个是学生id格式。我用fprintf写了它们的值,所以我在阅读时使用fscanf,但我的函数不能正常工作,你能告诉我我的错误在哪里吗?
(我删除了不能工作的旧代码,因为我继续使用不同的代码)
编辑:我试图根据下面的解决方案创建一个代码片段,但是当我进入while循环时,fscanf开始得到错误的数字。你能帮我看看我错在哪里吗?
编辑:编辑完这里,我做了fopen检查。它们没有问题。while循环在运行一次后关闭,我认为,在if fscanf部分,fscanf没有正确读取数字,所以它以break退出循环。
最可能损坏的部分:
FILE *index = fopen("index.txt", "r");
FILE *record = fopen("record.txt", "r");
if (!index)
return;
if (!record)
return;
int array[n][3];
//int *array = (int *)malloc(n *3 sizeof(int));???
int count = 0;
int i=0,temp,id,course,score;
while (1)
{
if(count==n) break;
if (fscanf(record, "%d", &id) != 1) break;
if (fscanf(record, "%d", &course) != 1) break;
if (fscanf(record, "%d", &score) != 1) break;
array[count][0] = id;
array[count][1] = course;
array[count][2] = score;
count++;
}
函数的其余部分,如果错误在其他地方,您可以浏览:
void sort_for_bin_search(int n)
{
FILE *index = fopen("index.txt", "r");
FILE *record = fopen("record.txt", "r");
if (!index)
return;
if (!record)
return;
int array[n][3];
//int *array = (int *)malloc(n *3 sizeof(int));???
int count = 0;
int i=0,temp,id,course,score;
while (1)
{
if(count==n) break;
if (fscanf(record, "%d", &id) != 1) break;
if (fscanf(record, "%d", &course) != 1) break;
if (fscanf(record, "%d", &score) != 1) break;
array[count][0] = id;
array[count][1] = course;
array[count][2] = score;
count++;
}
for (i = 1; i < n - 1; i++)
{
for (int j = 0; j < n - 1; j++)
{
if(array[i][0] > array [j][0])
{
temp=array[j][0];
array[j][0] = array[i][0];
array[i][0] = temp;
temp=array[j][1];
array[j][1] = array[i][1];
array[i][1] = temp;
temp=array[j][2];
array[j][2] = array[i][2];
array[i][2] = temp;
}
else if((array[i][0]==array[j][0])&&(array[i][1]>array[j][1]))
{
temp=array[j][0];
array[j][0] = array[i][0];
array[i][0] = temp;
temp=array[j][1];
array[j][1] = array[i][1];
array[i][1] = temp;
temp=array[j][2];
array[j][2] = array[i][2];
array[i][2] = temp;
}
}
}
fclose(record);
fclose(index);
FILE *index2 = fopen("index.txt", "w");
FILE *record2 = fopen("record.txt", "w");
for (i = 0; i < n; i++)
{
fprintf(index2,"%dn",array[i][0]);
fprintf(record2,"%d %d %dn",array[i][0],array[i][1],array[i][2]);
//printf("%d %d %dn",array[i][0],array[i][1],array[i][2]);
}
//free(array);
fclose(record2);
fclose(index2);
}
看起来您正在进行冒泡排序,并且在每次迭代中都从磁盘读取/写入。磁盘操作非常慢。如果你只读入一次数组,会更容易更快。然后对单个数组进行排序。
的例子:
void sort_for_bin_search(int n)
{
//assumes that `n` is the number of lines in this file
if (n < 1) return;
FILE* fin = fopen("index.txt", "r");
if (!fin)
return;
FILE* fout = fopen("record.txt", "w");
if (!fout)
return;
int* arr = malloc(n * sizeof(int));
if (!arr) return;
int count = 0;
while (1)
{
if (count == n)
break;
int id, course, score;
if (fscanf(fin, "%d", &id) != 1) break;
if (fscanf(fin, "%d", &course) != 1) break;
if (fscanf(fin, "%d", &score) != 1) break;
arr[count] = id;
count++;
}
//add code for sorting arr
for (int i = 0; i < count; i++)
fprintf(fout, "%dn", arr[i]);
free(arr);
fclose(fin);
fclose(fout);
}
然后你可以排序,例如使用冒泡排序。
使用printf
在屏幕上打印每一步的数据,这将有助于调试。
void sort_for_bin_search(int n)
{
FILE* fin = fopen("input_file.txt", "r");
if (!fin)
{
printf("input errorn");
return;
}
int array[n][3];
int count = 0;
while (1)
{
int id, course, score;
if (count == n) break;
if (fscanf(fin, "%d", &id) != 1) break;
if (fscanf(fin, "%d", &course) != 1) break;
if (fscanf(fin, "%d", &score) != 1) break;
array[count][0] = id;
array[count][1] = course;
array[count][2] = score;
count++;
}
n = count;
printf("reading:n");
for (int i = 0; i < n; i++)
printf("%d %d %dn", array[i][0], array[i][1], array[i][2]);
printf("nsortn");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n - 1 - i; j++)
{
if (array[j][0] > array[j + 1][0])
{
int temp;
temp = array[j][0];
array[j][0] = array[j + 1][0];
array[j + 1][0] = temp;
temp = array[j][1];
array[j][1] = array[j + 1][1];
array[j + 1][1] = temp;
temp = array[j][2];
array[j][2] = array[j + 1][2];
array[j + 1][2] = temp;
}
}
}
fclose(fin);
printf("sortedn");
for(int i = 0; i < n; i++)
printf("%d %d %dn", array[i][0], array[i][1], array[i][2]);
printf("write to filen");
FILE* fout = fopen("output_file.txt", "w");
if(!fout)
{
printf("output errorn");
return;
}
for (int i = 0; i < n; i++)
fprintf(fout, "%d %d %dn", array[i][0], array[i][1], array[i][2]);
fclose(fout);
}