计算C中文本文件中的整数个数

问题描述 投票:0回答:1

我制作了名为“qsort.txt”的文本文件,并在文件中写入任意多个整数(在我的例子中,准确地说是 35 个整数)。我的目标是计算该文件中有多少个整数,将其放入 malloc() 定义的数组中,并使用 qsort() 对其进行排序。之后我想将排序后的数字写入名为“sorted.txt”的文本文件中。

这是我的代码,但显然没有按预期工作。另外,我希望代码更加灵活,因此在 FILE *fa = fopen(argv[1],"r+") 中是“argv[1]”,这样我就可以放置任何其他文本文件进行排序。无论如何,我的问题是我不知道如何计算文件中有多少个整数。

#include<stdio.h>
#include<stdlib.h>

    int compare(const void *a, const void *b){
        if(*(int*)a < *(int*)b)
            return -1;
        else if (*(int*)a > *(int*)b)
            return 1;
        else
            return 0;
    }

int main(int argc, char **argv){

    FILE *fa = fopen(argv[1],"r+"); if(!fa) exit(1);
    FILE *fb = fopen("sortirano.txt","w+");

    long duljina, i;
    long *ptr, *zapis;
    long count = 0;

     while(!feof(fa)) count++;

     printf("%lu\n", count);

     fseek(fa, 0, SEEK_END);
     duljina = ftell(fa);
     fseek(fa, 0, SEEK_SET);

     ptr = (long*)malloc(sizeof(long));

     while(!feof(fa)){
         fscanf(fa, "%lu", ptr);
         count++;
     }
     printf("count: %lu\n", count);
     for(i = 0; i < count; i++)
         printf("%lu ", ptr[i]);

    printf("\n");

    free(ptr);

    fclose(fa);
    fclose(fb);

    return 0;
}
c file text malloc argv
1个回答
0
投票

由于文件仅包含整数和空格,因此您可以使用

fscanf("%ld", ...)
解析它,并将数字存储到您读取更多数字时有意重新分配的数组中。

请注意以下备注:

  • 您的比较功能可以与

    qsort
    一起使用。

  • 无需在模式字符串中使用

    +
    打开文件进行更新,

  • 也不需要查找文件末尾。

  • 使用

    while(!feof(fa))
    测试文件结尾总是错误的。您应该测试
    fscanf()
    是否成功并根据需要重新分配数组。

这是修改后的版本:

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int compare(const void *aa, const void *bb) {
    const long int *a = aa;
    const long int *b = bb;
    return (*a > *b) - (*a < *b);
}

int main(int argc, char **argv) {
    if (argc < 2) {
        fprintf(stderr, "missing argument\n");
        return 1;
    }
    FILE *fa = fopen(argv[1], "r");
    if (!fa) {
        fprintf(stderr, "cannot open %s: %s\n", argv[1], strerror(errno));
        return 1;
    }

    long *ptr = NULL;
    size_t alloc = 0, count = 0;
    long value;

    while (fscanf(fa, "%ld", &value) == 1) {
        if (count == alloc) {
            size_t new_alloc = alloc + alloc / 2 + 32;
            long *new_ptr = realloc(ptr, sizeof(*ptr) * alloc);
            if (!new_ptr) {
                fprintf(stderr, "out of memory for %zu entries\n", new_alloc);
                free(ptr);
                fclose(fa);
                return 1;
            }
            ptr = new_ptr;
            alloc = new_alloc;
        }
        ptr[count++] = value;
    }
    fclose(fa);

    printf("count: %zu\n", count);

    qsort(ptr, count, sizeof(*ptr), compare);

    const char *outfile = "sorted.txt";
    FILE *fb = fopen(outfile, "w");
    if (!fb) {
        fprintf(stderr, "cannot open %s: %s\n", outfile, strerror(errno));
        free(ptr);
        return 1;
    }

    for (size_t i = 0; i < count; i++) {
        fprintf(fb, "%ld\n", ptr[i]);
    }

    fclose(fb);
    free(ptr);

    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.