我想将 .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 包含第一列和最左边列中的所有行,依此类推。
最佳答案
fscanf
是你的 friend 吗:
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);
然后,行应该包含您需要的数据。
关于c - 从 .txt 文件读取到 C 数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10467278/