簡體   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