如何从文件中读取大量列到 C 中的数组?
How to read large number of columns from file to arrays in C?
我想使用 C 从文件读取大量(未知列数)到二维数组。每行的列数是常量。列在文件中由一个 space (' ') 分隔。
我尝试在循环中使用循环,但没有成功。我还设法编写了一个计算列数的代码,但就我而言这是如此。我是编程的初学者,所以任何帮助将不胜感激。
数据为txt格式,以space字符分隔,每行偶数列。我尝试将所有数据读取到二维数组,因为我得出的结论是这种格式以后会更容易使用。
我使用的代码如下
int readData(char *filename, FILE *f) {
int lines;
int count = 0;
float dataArray [lines][count];
int i,j;
char c;
f = fopen(filename, "r");
if (f == NULL) return -1;
for (c = getc(f); c != '\n'; c = getc(f)) {
if (c == '\t')
count++;
}
printf("Your file %s consists of %d columns\n", filename, count);
printf("Set the lenght: ");
scanf("%d", &lines);
for(int i=0;i<lines;i++){
for(int j=0;j<count;j++){
fscanf(f,"%f", &dataArray[i][j]);
}
}
for (i=0; i<lines; i++) {
for (j=0;j<count;j++)c{
printf("%.3f",dataArray[i][j]);
}
printf("\n");
}
printf("\n");
fclose(f);
return 0;
}
int main (int argc, char *argv[], char filename[512]) {
FILE *f;
printf("Enter the file name: ");
if (scanf("%s", filename) != 1) {
printf("Error in the filename\n");
return -1;
}
if (readData(filename, f) == -1) {
printf("Error in file opening\n");
return -1;
}
return 0;
}
数据输入是这样的:
217,526 299,818 183,649 437,536 213,031 251 263,275 191,374 205,002 193,645 255,846 268,866 2,516\n
229,478 304,803 184,286 404,491 210,738 237,297 279,272 189,44 202,956 204,126 242,534 276,068 2,163\n
但从中我得到了一个理想长度的数组,但其余的都是错误的,看起来像这样:
225.0000000.000000-1798259351694660865572287994621067264.0000000.0000000.0000000.0000000.0000000.0000000.0000000.00000014037667868815752572174336.0000000.000000\n
225.0000000.000000-1798259351694660865572287994621067264.0000000.0000000.0000000.0000000.0000000.0000000.0000000.00000014037667868815752572174336.0000000.000000\n
您的目标数组必须是动态的。您正在使用未初始化的数据创建静态数组:
float dataArray [lines][count]; // line is not initialized, and count = 0
改成这样:
声明:
float dataArray**;
初始化:
// only after setting count and lines
dataArray = malloc(lines * sizeof(float*));
for (int i = 0; i < lines; ++i) {
dataArray[i] = malloc(count * sizeof(float));
}
清理:您必须在不再需要时释放分配给 dataArray
的内存:
for (int i = 0; i < lines; ++i) {
free(dataArray[i]);
}
free(dataArray);
dataArray = NULL; // It's good to get used to nullify vars after deleting them
我想使用 C 从文件读取大量(未知列数)到二维数组。每行的列数是常量。列在文件中由一个 space (' ') 分隔。
我尝试在循环中使用循环,但没有成功。我还设法编写了一个计算列数的代码,但就我而言这是如此。我是编程的初学者,所以任何帮助将不胜感激。
数据为txt格式,以space字符分隔,每行偶数列。我尝试将所有数据读取到二维数组,因为我得出的结论是这种格式以后会更容易使用。
我使用的代码如下
int readData(char *filename, FILE *f) {
int lines;
int count = 0;
float dataArray [lines][count];
int i,j;
char c;
f = fopen(filename, "r");
if (f == NULL) return -1;
for (c = getc(f); c != '\n'; c = getc(f)) {
if (c == '\t')
count++;
}
printf("Your file %s consists of %d columns\n", filename, count);
printf("Set the lenght: ");
scanf("%d", &lines);
for(int i=0;i<lines;i++){
for(int j=0;j<count;j++){
fscanf(f,"%f", &dataArray[i][j]);
}
}
for (i=0; i<lines; i++) {
for (j=0;j<count;j++)c{
printf("%.3f",dataArray[i][j]);
}
printf("\n");
}
printf("\n");
fclose(f);
return 0;
}
int main (int argc, char *argv[], char filename[512]) {
FILE *f;
printf("Enter the file name: ");
if (scanf("%s", filename) != 1) {
printf("Error in the filename\n");
return -1;
}
if (readData(filename, f) == -1) {
printf("Error in file opening\n");
return -1;
}
return 0;
}
数据输入是这样的:
217,526 299,818 183,649 437,536 213,031 251 263,275 191,374 205,002 193,645 255,846 268,866 2,516\n
229,478 304,803 184,286 404,491 210,738 237,297 279,272 189,44 202,956 204,126 242,534 276,068 2,163\n
但从中我得到了一个理想长度的数组,但其余的都是错误的,看起来像这样:
225.0000000.000000-1798259351694660865572287994621067264.0000000.0000000.0000000.0000000.0000000.0000000.0000000.00000014037667868815752572174336.0000000.000000\n
225.0000000.000000-1798259351694660865572287994621067264.0000000.0000000.0000000.0000000.0000000.0000000.0000000.00000014037667868815752572174336.0000000.000000\n
您的目标数组必须是动态的。您正在使用未初始化的数据创建静态数组:
float dataArray [lines][count]; // line is not initialized, and count = 0
改成这样:
声明:
float dataArray**;
初始化:
// only after setting count and lines
dataArray = malloc(lines * sizeof(float*));
for (int i = 0; i < lines; ++i) {
dataArray[i] = malloc(count * sizeof(float));
}
清理:您必须在不再需要时释放分配给 dataArray
的内存:
for (int i = 0; i < lines; ++i) {
free(dataArray[i]);
}
free(dataArray);
dataArray = NULL; // It's good to get used to nullify vars after deleting them