C语言 如何从文本文件中读取并存储在c中的矩阵中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18215325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:09:21  来源:igfitidea点击:

how to read from text file and store in matrix in c

ctextmatrix

提问by rooooookie

the first to say is that I'm totally new to coding, so plz forgive my mistakes. I'm now trying to read from a txt file which is rather large, it has about 1000000 lines and 4 cols

首先要说的是我对编码完全陌生,所以请原谅我的错误。我现在试图从一个相当大的 txt 文件中读取,它有大约 1000000 行和 4 个列

56.154 59.365 98.3333 20.11125
98.54 69.3645 52.3333 69.876
76.154 29.365 34.3333 75.114
37.154 57.365 7.0 24.768
........
........

I want to read them all and store them into a matrix, here is my code:

我想全部读取它们并将它们存储到矩阵中,这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

int main()
{
  int i;
  int j;

/*matrix*/
int** mat=malloc(1000000*sizeof(int));
for(i=0;i<1000000;++i)
mat[i]=malloc(4*sizeof(int));


  FILE *file;
  file=fopen("12345.txt", "r");

 for(i = 0; i < 1000; i++)
  {
      for(j = 0; j < 4; j++) 
      {
       if (!fscanf(file, " %c", &mat[i][j])) 
           break;
       mat[i][j] -= '0'; /* I found it from internet but it doesn't work*/
       printf("\n",mat[i][j]);
      }

  }
  fclose(file);
}

The result is that I got nothing in my matrix. I hope u can help. Thanks in advance for any help.

结果是我的矩阵中什么也没有。我希望你能帮忙。在此先感谢您的帮助。



回答by P0W

Many Issues, consider following, and of course see comments

许多问题,请考虑关注,当然也可以查看评论

int main()
{
  int i;
  int j;

/*matrix*/
/*Use double , you have floating numbers not int*/

double** mat=malloc(1000000*sizeof(double*)); 
for(i=0;i<1000000;++i)
mat[i]=malloc(4*sizeof(double));


  FILE *file;
  file=fopen("1234.txt", "r");

 for(i = 0; i < 1000; i++)
  {
      for(j = 0; j < 4; j++) 
      {
  //Use lf format specifier, %c is for character
       if (!fscanf(file, "%lf", &mat[i][j])) 
           break;
      // mat[i][j] -= '0'; 
       printf("%lf\n",mat[i][j]); //Use lf format specifier, \n is for new line
      }

  }
  fclose(file);
}

回答by Ingo Leonhardt

  1. fscanf( "%c", ... )only scans one single character (e.g. '5'). By subtracting '0' you get the integer value 5from the character '5'. You can use "%d"to scan integers that consist only of digits (not including formatting characters), or "%f"for floats (not sure if 56.154is to be read as "56 thousand 154" (continental europe) or "56 plus 154/1000" (GB / USA) (the rest of the world: don't be offended I just don't know)

  2. printf( "\n", ... ): you forgot to use any formatting string such as %d(int), %f(float) ... So your parameter will not be printed, just the newline itself.

  3. int** mat=malloc(1000000*sizeof(int));You're allocating an array of int *here, so it should be int** mat=malloc(1000000*sizeof(int *));

  1. fscanf( "%c", ... )只扫描一个字符(例如'5')。通过减去“0”,您可以5从字符中获得整数值'5'。您可以使用"%d"扫描仅由数字组成的整数(不包括格式化字符),或"%f"用于浮点数(不确定56.154是读作“56,000 154”(欧洲大陆)还是“56 plus 154/1000”(GB /美国)(世界其他地区:别生气,我只是不知道)

  2. printf( "\n", ... ):您忘记使用任何格式字符串,例如%d(int), %f(float) ... 所以您的参数不会被打印,只是换行符本身。

  3. int** mat=malloc(1000000*sizeof(int));你在int *这里分配一个数组,所以它应该是int** mat=malloc(1000000*sizeof(int *));

Edit:I've looked again at your text file and have seen numbers like 98.54 that can't be formatted integers. So it's quite clear you will need floator doubleinstead if intfor your array and use "%f"for floator "%lf"for doublein both fscanf()and printf()

编辑:我再次查看了您的文本文件,并看到了无法格式化为整数的 98.54 之类的数字。因此,它是很清楚你需要floatdouble替代,如果int你的阵列和使用"%f"float"%lf"double两个fscanf()printf()

回答by sedavidw

You have a couple of things wrong with your code here.

您的代码在这里有一些问题。

First off you create a matrix of intyet you are reading in float values it looks like. You probably want to use double

首先,您创建一个矩阵,int但您正在读取它看起来像的浮点值。你可能想用double

Second, when you are reading a double you should use

其次,当你在读一个 double 时,你应该使用

fscanf(file, "%lf", &some_double);  // fscanf(file, "%d", &some_int); for integers

Also when you are allocating your matrix, your first mallocyou should pass

此外,当您分配矩阵时,您malloc应该首先通过

sizeof(double *) // or int * if you are really trying to use integers

Finally your line that does:

最后你的行:

mat[i][j] -= '0'

What are you trying to accomplish here? You're taking the int that you (tried) to read in and subtracting off '0'...

你想在这里完成什么?您正在使用您(尝试)读入的 int 并减去“0”......

EditI also notice that you are hard coding the number of lines you are reading, I wouldn't do that unless you know the format of the file.

编辑我还注意到您正在对正在阅读的行数进行硬编码,除非您知道文件的格式,否则我不会这样做。