简体   繁体   English

从文本文件读取数字到2D数组

[英]Read numbers from text file to 2D array

Despite the numerous examples on here I can't seem to get this working... 尽管这里有很多例子我似乎无法让这个工作......

I have a text file, containing many rows, each row has three (int) values separated by a single space. 我有一个文本文件,包含许多行,每行有三个(int)值,由一个空格分隔。 For example: 例如:

1 0 0
0 0 0
1 0 1
0 0 2
1 0 2

I am trying to read this into a 2d array. 我试图把它读成2d数组。

My code so far: 我的代码到目前为止:

    int main(void)
    {
       char c;
       int i = 0;
       int maxLines = 18;
       char lines[maxLines][BUFSIZ];
       FILE *fp = fopen("inputs/control.txt", "r");

       if (fp == 0)
       {
           fprintf(stderr, "failed to open inputs/control.txt\n");
           exit(1);
       }

       char buffer[maxLines];
       while (i < maxLines && fgets(buffer[i], sizeof(buffer[0]), fp))
       {
          sscanf (buffer, "%d %d %d", &lines[i][0], &lines[i][1], &lines[i][2]);    
          i++;
       }

       fclose(fp);
       return 0;
    }

Can someone provide advice how I can develop this further so that each value in the row is stored in a separate array index? 有人可以提供建议我如何进一步开发这个,以便行中的每个值存储在一个单独的数组索引中? In my example above, we would store something like: 在上面的例子中,我们将存储类似于:

lines[0][0] = 1
lines[0][1] = 0
lines[0][2] = 0
and so on.....

Currently it stores the entire row in a single array pos. 目前,它将整行存储在单个数组pos中。

As you can probably tell I'm a C noob, any help would be fantastic! 你可能会说我是一个C菜鸟,任何帮助都会很棒!

Use scanf . 使用scanf Example: 例:

#include <stdio.h>

int main(int argc, char *argv[]) {
    int i, j;
    int lines[18][3];
    i = 0;
    while (
        i != sizeof(lines) / sizeof(lines[0])
     && 3 == scanf("%i %i %i", lines[i] + 0, lines[i] + 1, lines[i] + 2)
    ) {
        i++;
    }
    for (j = 0; j !=i; j++) {
        printf("%i %i %i\n", lines[j][0], lines[j][1], lines[j][2]);
    }
    return 0;
}

Note that the input is read from stdin (use fscanf for more flexibility), meaning that the snippet above must be called as ./a.out < data.txt . 请注意,输入是从stdin读取的(使用fscanf以获得更大的灵活性),这意味着必须将上面的代码段调用为./a.out < data.txt

You were well on your way, you just had problems thinking you were reading a character array instead of an array of signed characters (which could be changed to int, etc) Here is your example: 你很顺利,你只是想到你正在读一个字符数组而不是一个有符号字符数组(可以改为int等),这是你的例子:

#include <stdio.h>

#define MAXB 32
#define MAXL 18
#define MAXD 3

int main(void)
{
    int i = 0;
    int numlines = 0;
    char buf[MAXB] = {0};
    char lines[MAXL][MAXD];

    FILE *fp = fopen("inputs/control.txt", "r");

    if (fp == 0)
    {
       fprintf(stderr, "failed to open inputs/control.txt\n");
       return 1;
    }

    while (i < MAXL && fgets (buf, MAXB - 1, fp))
    {
        if (sscanf (buf, "%hhd %hhd %hhd", &lines[i][0], &lines[i][1], &lines[i][2]) == 3)
            i++;
    }

    fclose(fp);

    numlines = i;
    int j = 0;

    for (i = 0; i < numlines; i++)
        for (j = 0; j < MAXD; j++)
            printf (" line[%2d][%2d] : %hhd\n", i, j, lines[i][j]);

    printf ("\n");

    return 0;
}

Output 产量

$ ./bin/read_array_a3
 line[ 0][ 0] : 1
 line[ 0][ 1] : 0
 line[ 0][ 2] : 0
 line[ 1][ 0] : 0
 line[ 1][ 1] : 0
 line[ 1][ 2] : 0
 line[ 2][ 0] : 1
 line[ 2][ 1] : 0
 line[ 2][ 2] : 1
 line[ 3][ 0] : 0
 line[ 3][ 1] : 0
 line[ 3][ 2] : 2
 line[ 4][ 0] : 1
 line[ 4][ 1] : 0
 line[ 4][ 2] : 2

Note: char lines[MAXL][MAXD]; 注意: char lines[MAXL][MAXD]; is fine, you just must understand that each element is restricted to an 8-bit signed value, meaning values between -128 < val < 127 . 没关系,您必须明白每个元素都限制为8位有符号值,这意味着-128 < val < 127之间的值。 You can make them int if you need to store larger values. 如果需要存储更大的值,可以将它们设为int

Using fgets you are reading the whole line from the file. 使用fgets,您正在从文件中读取整行。 You will need to then parse this line to extract individual numbers and store them into the array. 然后,您需要解析此行以提取单个数字并将其存储到数组中。 In the while loop you can read the line in a buffer and then use something like strtok/sscanf to parse each number. 在while循环中,您可以读取缓冲区中的行,然后使用类似strtok / sscanf的内容来解析每个数字。

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

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