1

我想将存储在由空格分隔的 .txt 文件中的 4 列中的每一列读入它们自己的数组中。文本文件可能有数百行,因此需要读取到文件末尾。

例子:

3.4407280e+003 6.0117545e+003 8.0132664e+002 2.5292922e+003
3.4163843e+003 5.9879421e+003 7.7792044e+002 2.5058547e+003

所以通常它是数组 1,包含第一列和最左列中的所有行,依此类推。

4

1 回答 1

2

fscanf是你的朋友:

static const int MAX_FILE_ROWS = 200;

double lines[MAX_FILE_ROWS][4];
FILE *file = fopen("myfile.txt", "r");

for (int i = 0; i < MAX_FILE_ROWS; i++)
{
    if (feof(file))
        break;

    fscanf(file, "%lf %lf %lf %lf", &(lines[i][0]), &(lines[i][1]), &(lines[i][2]), &(lines[i][3]));
}

fclose(file);

然后,行应该包含您需要的数据。

于 2012-05-06T01:10:43.877 回答