首页 > 解决方案 > 如何读取输入文件并将整数存储到C中的数组中

问题描述

我正在使用 C 语言开发 CPU 调度程序模拟器,并且我有一个包含以下内容的输入文件:

1 0 10
2 0 9
3 3 5
4 7 4
5 10 6
6 10 7

我想以以下方式将数字分别存储在数组中的第一、第二和第三列中:

a1[0] = 1     a2[0] = 0     a3[0] = 10
a1[1] = 2     a2[1] = 0     a3[1] = 9
a1[2] = 3     a2[2] = 3     a3[2] = 5
a1[3] = 4     a2[3] = 7     a3[3] = 4
a1[4] = 5     a2[4] = 10    a3[4] = 6
a1[5] = 6     a2[5] = 10    a3[5] = 7  

我无法理解如何通过 C 实现来实现这一点。我目前的代码如下:

int pList[len], wtList[len], btList[len];

char *str[256];
char *token[30];

FILE *input;

input = fopen(argv[1], "r");

if (input == NULL) {
    printf("File not found. \n");
    exit(1);
}

while (fgets(str, sizeof str, input)) {
    if (sscanf(str, "%d %d %d", &pList, &wtList, &btList) == 3) { 
        totalJobs++;
    } else {
        printf("Input text is not in 3 column format. Please check. \n");
    }
}

fclose(input);

//printf("Scheduling algorithm: %s\n", argv[2]);
//printf("Total %d tasks are read from \"%s\". press 'enter' to start...\n", totalJobs, input);
//printf("==================================================================\n");
for (i = 0; i < 6; i++) {
    printf(pList[i]);
    printf(wtList[i]);
    printf(btList[i]);
}

理想情况下,我想要一个将这些数字放入数组的抽象实现,因为输入文件应该能够有更多或更少的行并且仍然可以工作。如果我的格式已关闭,我深表歉意,因为我是使用 Stack Overflow 的新手。提前感谢您的帮助,如果我的问题仍然不清楚,请告诉我。

更新 1:我按照说明进行了以下更正,现在出现Segmentation fault (Core dumped)错误。可能是因为我没有*在数组前面吗?

标签: cfile-io

解决方案


你的代码有很多问题。

1)您的数组未初始化,这是获得不可读输出和可能非常糟糕的错误的可靠方法。

2) 代码中的一些变量未声明。将来,请按原样发布代码,以便立即编译。没有得到任何问题答案的一种经过验证的真实方法是发布需要在编译之前进行大量编辑的代码:人们通常只是“搞砸了,不值得”,你真的不能责怪他们。

3)您读取文件的方式在性能方面的代码结构方面效率低下。读取文件的最佳方法是一口气读取它们,然后解析它们以从中获取所需的内容。

4)您在最后一个 for 循环中的 printfs 完全错误,这很奇怪,因为在上面注释掉的 printfs 以及 sscanf 中,您似乎了解格式字符串的工作原理。我认为这是没有仔细阅读您的代码的错误。请从现在开始这样做。

这是一个完整的程序,我认为它可以大致满足您的要求:

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

int main(int argc, char** argv) {
    #define jobArrayLength (6ULL)
    #define stringLength   (256ULL)
    int  pList[jobArrayLength] = {0}, wtList[jobArrayLength] = {0}, btList[jobArrayLength] = {0};
    int  totalJobs = 0; // Used to not be declared
    char str[stringLength] = {0} ; // Used to be uninitialized

    FILE *input;
    input = fopen(argv[1], "r"); 
    if (input == NULL) {
        printf("File not found. \n");
        exit(1);
    }

    fread(str, 1, stringLength, input); // Used to be sizeof str which is wrong
    fclose(input);
    int i; // Used to not be declared
    int stringIndex = 0;
    for (i = 0; i < jobArrayLength; i++) {
        if (sscanf(str + stringIndex, "%d %d %d", pList + i, wtList + i, btList + i) == 3) { // used to not index into the array
            totalJobs++;
            while (str[stringIndex++] != '\n' && stringIndex != stringLength) {} // Skip to next row of numbers
        } else {
            printf("Input text is not in 3 column format. Please check. \n");
        }
    }

    //printf("Scheduling algorithm: %s\n", argv[2]);
    //printf("Total %d tasks are read from \"%s\". press 'enter' to start...\n", totalJobs, input);
    //printf("==================================================================\n");
    for (i = 0; i < jobArrayLength; i++) {
        printf("%d, %d, %d\n", pList[i], wtList[i], btList[i]); // Used to just print an int array element, which is not how printf works.
    }
    getchar();
    return 0;
}

推荐阅读