[英]Sorting structure variables(3 ints) in file from smallest to largest C
I need to sort my 2 text files for my project.我需要为我的项目排序我的 2 个文本文件。 One of the text files is in student id, course id and score format and the other is in student id's format.
其中一个文本文件采用学生 ID、课程 ID 和分数格式,另一个采用学生 ID 格式。 I wrote the values in both of them with fprintf, so I am using fscanf while reading, but my function is not working correctly, can you tell me where is my mistake?
我用fprintf写了它们两个中的值,所以我在阅读时使用了fscanf,但我的功能无法正常工作,你能告诉我我的错误在哪里吗?
(I deleted the old code that didn't work because I continued on a different code) (我删除了无效的旧代码,因为我继续使用不同的代码)
Edit :I tried to create a code snippet based on the solution below, but when I enter the while loop, fscanf starts to get the wrong numbers.编辑:我试图根据下面的解决方案创建一个代码片段,但是当我进入 while 循环时,fscanf 开始得到错误的数字。 Can you look for where my mistake is?
你能找出我的错误在哪里吗?
Edit :After editing here, I did the fopen checks.编辑:在这里编辑后,我做了 fopen 检查。 There is no problem with them.
他们没有问题。 The while loop closes after it runs once, I think, in the if fscanf parts, fscanf does not read the number correctly, so it exits the loop with break.
while 循环在运行一次后关闭,我认为,在 if fscanf 部分,fscanf 无法正确读取数字,因此它会以 break 退出循环。
most likely broken part:最有可能损坏的部分:
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++;
}
the rest of the function for you to browse if the error is elsewhere:其余功能供您浏览是否错误在其他地方:
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,"%d\n",array[i][0]);
fprintf(record2,"%d %d %d\n",array[i][0],array[i][1],array[i][2]);
//printf("%d %d %d\n",array[i][0],array[i][1],array[i][2]);
}
//free(array);
fclose(record2);
fclose(index2);
}
It looks like you are doing a bubble sort, and in each iteration you read/write from disk.看起来您正在进行冒泡排序,并且在每次迭代中您都从磁盘读取/写入。 Disk operations are very slow.
磁盘操作非常缓慢。 It is much easier and faster if you read once in to array.
如果您一次读入数组,它会更容易和更快。 And then sort that single array.
然后对该单个数组进行排序。
Example:例子:
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, "%d\n", arr[i]);
free(arr);
fclose(fin);
fclose(fout);
}
Then you can sort, for example using bubble sort.然后你可以排序,例如使用冒泡排序。
Use printf
to print the data on screen at each step, this will help with debugging.在每一步使用
printf
在屏幕上打印数据,这将有助于调试。
void sort_for_bin_search(int n)
{
FILE* fin = fopen("input_file.txt", "r");
if (!fin)
{
printf("input error\n");
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 %d\n", array[i][0], array[i][1], array[i][2]);
printf("\nsort\n");
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("sorted\n");
for(int i = 0; i < n; i++)
printf("%d %d %d\n", array[i][0], array[i][1], array[i][2]);
printf("write to file\n");
FILE* fout = fopen("output_file.txt", "w");
if(!fout)
{
printf("output error\n");
return;
}
for (int i = 0; i < n; i++)
fprintf(fout, "%d %d %d\n", array[i][0], array[i][1], array[i][2]);
fclose(fout);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.