Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中对.txt文件中的数字进行排序_C_File_Sorting - Fatal编程技术网

在C中对.txt文件中的数字进行排序

在C中对.txt文件中的数字进行排序,c,file,sorting,C,File,Sorting,我做了一个排序算法,当我需要排序的数字在代码中的数组中时,它可以正常工作,如下所示: int sort[] = {16,8,23,4,42,15}; FILE* f; f = fopen("1000.txt", "r"); if(f == 0){ printf("Database unavaible or corrupted\n\n"); exit(1); } 但是我需要代码来对.txt文件中的数字进行排序,我知道文件的大小(因此不需要大小来知道需要排序多少个数字),但问题是

我做了一个排序算法,当我需要排序的数字在代码中的数组中时,它可以正常工作,如下所示:

int sort[] = {16,8,23,4,42,15};
FILE* f;
f = fopen("1000.txt", "r");
if(f == 0){
    printf("Database unavaible or corrupted\n\n");
    exit(1);
}
但是我需要代码来对.txt文件中的数字进行排序,我知道文件的大小(因此不需要大小来知道需要排序多少个数字),但问题是文件中的数字不是用逗号分隔的,而是用空格分隔的,我不知道如何让我的代码使用这个数字列表

我的代码是这样的,就像我说的,当数字的排序数组位于由comas分隔的代码中时,它就会工作:

int main(){

    int temp, size;
    int sort[] = {16,8,23,4,42,15};
    size = sizeof(sort) / sizeof(int);

    for(int j = 0; j < size; j++){
        for(int i = 0; i < size; i++){
            if(sort[i] > sort[i+1]){
                temp = sort[i];
                sort[i] = sort[i+1];
                sort[i+1] = temp;
            }
        }
    }

    for(int p = 0; p < size; p++){
        printf("%d ", sort[p]);
    }
}

但我不知道下一步该怎么办,我该如何得到这个没有用逗号分隔的未排序数字的文件,并使我的代码排序并打印它们?

您可以使用
fscanf
从文件中读取数字,然后将这些数字存储在一个数组中。最后,您可以使用排序函数对该数组中的数据进行排序

下面的代码是从文件中读取数字的示例:

#include <stdio.h>
#define MAX_NUM 10

int main() {
    FILE * fp = fopen("input.txt", "r");
    if(!fp) {return -1;}
    int array[MAX_NUM] = {0}; // you can use array[size] if you know exactly how many numbers in the file 
    int i = 0;
    while(i < MAX_NUM && fscanf(fp, "%d", &array[i]) == 1) {
        printf("a[%d] = %d\n", i, array[i]);
        i++;
    }

    // sort the array here as you did in your code
    return 0;
}
i=size-1
时,
sort[i+1]
将成为
sort[size]
数组
sort
,因为您可以访问的最大索引是
size-1
而不是
size
。应改为:

for(int j = 0; j < size; j++) {
    for(int i = 0; i < size-j-1; i++) {...}
}
for(int j=0;j
要读取数字,可以使用
fscanf
,但仍存在内存分配问题

int *array = (int*)malloc(10*sizeof(int));
unsigned numbers_counter = 0;

while(fscanf(f,"%i",&array[numbers_counter++])==1){
    if(numbers_counter>=sizeof(array)){
        if((array = (int*) realloc(array,sizeof(int)*sizeof(int)*10)) == NULL)
            exit(1);
    }
}
假设该文件有20个元素,但最大值为4096,则不必要地占用4086*4字节的内存。 另一方面,该文件可以有8000个元素,但只能存储4096个元素。 您可以做的是动态内存分配

int *array = (int*)malloc(10*sizeof(int));
unsigned numbers_counter = 0;

while(fscanf(f,"%i",&array[numbers_counter++])==1){
    if(numbers_counter>=sizeof(array)){
        if((array = (int*) realloc(array,sizeof(int)*sizeof(int)*10)) == NULL)
            exit(1);
    }
}
在这段代码中,我要做的是在内存中分配一个10 int的空间,然后,当数组已满(
if(numbers\u counter>=sizeof(array))
)时,为数组分配一组更大的数字。 如果你有10个数字,它只占用10*4字节,从11到100它占用100*4字节,以此类推