将文本文件中的每一列数据存储到C中的数组中

Storing each column of data from a text file into array in C

我有一个文本文件名 file.txt,其中包含 2 列数字,例如,

10 1
20 2
30 3
40 4
50 5

我希望能够将数据列存储到一个数组中,以便我可以在其他地方使用这些数据。目前,代码在底部:

int main()
{
    int i = 0, j = 0, numofProcesses = 0;
    char *token;    

    // reading the textfile
    char *filename = "file.txt";
    FILE *fp = fopen(filename, "r");

    if (fp == NULL)
    {
        printf("Error: could not open file %s", filename);
        return 1;
    }
    else
    {
        // reading line by line, max 256 bytes
        const unsigned MAX_LENGTH = 256;
        char SingleLine[MAX_LENGTH];

        while (fgets(SingleLine, MAX_LENGTH, fp))
        {
            // num of Processes = count the number of line
            numofProcesses += 1;

            // print each line out
            // printf("%s", SingleLine);

            // remove trailing new line
            SingleLine[strcspn(SingleLine, "\n")] = 0;

            // print out each number in the text file base on the line
            // Returns first token 
            token = strtok(SingleLine, " ");
            // Keep printing tokens while one of the
            // delimiters present in str[].
            while (token != NULL)
            {
                printf("%s\n", token);
                token = strtok(NULL, " ");
                
            }
        }
            
        // check number of processes    
        printf("\n\nNumber of process = %d", numofProcesses);

        // close the file
        fclose(fp);
    }
    printf("\n");
    return 0;
} 

我能得到:

10
1
20
2
30
3
40
4
50
5


Number of process = 5

但是,我想要获得的输出存储在 2 个不同的数组中:

array a = [10, 20, 30, 40, 50]
array b = [1, 2, 3, 4, 5]

我可以知道是否可以单独获取数组吗? 谢谢

是的。你能行的。我对你的代码做了一些修改(见评论):

#include <stdio.h>
#include <string.h>
#include <errno.h> // For errno

// Either: int main (int argc, const char *argv[])
// Or:     int main(void)

int main(void)
{
    // reading the textfile
    char *filename = "file.txt";
    FILE *fp = fopen(filename, "r");

    // Check for errors.
    // Use perror("") or strerror(errno) for more meaningful error messages
    if (!fp) {
        printf("Error opening \"%s\": %s.\n", filename, strerror(errno));
        return 1;
    }

    // Always declare your variables near where you use them.

    int numofProcesses = 0;

    // reading line by line, max 256 bytes
    const unsigned MAX_LENGTH = 256;
    char SingleLine[MAX_LENGTH];
    
    // The two arrays you wanted
    const int array_size = 256;
    int a1[array_size], a2[array_size];
    
    while (fgets(SingleLine, MAX_LENGTH, fp))
    {
        // Remove trailing new line
        size_t newline_pos = strcspn(SingleLine, "\n"); // But what if \n is not found?
        SingleLine[newline_pos] = 0;
        
        // Parse the line read
        if (sscanf(SingleLine, "%d %d", &a1[numofProcesses], &a2[numofProcesses]) != 2) {
            // Problematic line...
        }
        
        // num of Processes = count the number of line
        numofProcesses += 1;
    }
    
    // close the file as soon as you are done with it.
    fclose(fp);
        
    // check number of processes    
    printf("Number of process = %d\n", numofProcesses);
    
    // Print to see if they match the numbers in file.txt
    int i;
    for (i = 0; i < numofProcesses; i++)
        printf("%d %d\n", a1[i], a2[i]);
}

输出:

Number of process = 5
10 1
20 2
30 3
40 4
50 5