繁体   English   中英

如何在C中读取输入文件并将整数存储到数组中

[英]How to read input file and store integers into array in C

我正在使用C中的CPU Scheduler模拟器,我有一个输入文件,其中包含以下内容:

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)错误。 可能是因为我在阵列前面没有*吗?

你的代码有很多问题。

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM