C 从.txt文件读取矩阵
这对我来说有点难,因为我发现的教程并不是很有用,我在这里找到的答案也很混乱。。。我有一个.txt文件:C 从.txt文件读取矩阵,c,matrix,C,Matrix,这对我来说有点难,因为我发现的教程并不是很有用,我在这里找到的答案也很混乱。。。我有一个.txt文件: 1 5 4 7 8 9 6 5 4 7 8 9 4 8 7 5 2 6 9 8 5 4 4 7 3 3 2 5 9 9 7 4 5 6 9 8 1 7 3 6 5 4 7 8 5 1 4 2 9 5 1 2 3 5 7 8 4 6 5 5 4 5 9 6 8 2 3 4
1 5 4 7 8 9 6 5 4 7 8 9
4 8 7 5 2 6 9 8 5 4 4 7
3 3 2 5 9 9 7 4 5 6 9 8
1 7 3 6 5 4 7 8 5 1 4 2
9 5 1 2 3 5 7 8 4 6 5 5
4 5 9 6 8 2 3 4 8 1 6 3
8 4 5 3 2 0 1 2 6 9 8 7
0 2 3 5 4 8 9 5 1 5 6 5
1 2 0 4 5 9 3 5 7 1 9 4
4 8 9 5 6 7 8 4 9 1 5 2
6 3 5 9 8 4 2 3 5 6 7 8
3 0 2 9 4 0 5 8 9 7 3 1
我需要把它转换成一个
int matr[12][12];
如何构造代码,以便将所有这些数字放入矩阵中?
我知道我应该使用fscanf和指针,但我经常会遇到这样的错误
无法将参数“1”的“const char*”转换为“FILE*{aka_iobuf*}”转换为“int fscanf(FILE*,const char*,…)”
因此,请在这里给我一些说明。根据您提供的内容(很少),我认为您的问题是fscanf()
需要一个文件*
作为其第一个参数,但您提供的文件名是常量字符*
您可以使用fopen()
获取文件*
FILE*fileptr=fopen(“,“r”);//打开文件进行读取
如果(fileptr==NULL)/*执行一些错误处理工作*;
int i,j;
对于(i=0;i<12;++i)
对于(j=0;j<12;++j)
fscanf(fileptr、%d、&matr[i][j]);
//一些重要的矩阵材料。。。
fclose(fileptr);//完成后关闭文件
我建议您提供一些源代码来显示您正在执行的操作以及错误的来源。不是“转换”,而是“创建数组并用从文件读取的值填充”
FILE * fileptr = fopen("<name of file in double quotes>", "r"); // open file for reading
if (fileptr == NULL) /* do some error handling stuff */;
int i, j;
for (i = 0; i < 12; ++i)
for (j = 0; j < 12; ++j)
fscanf(fileptr, "%d", &matr[i][j]);
// Some important matrix stuff...
fclose(fileptr); // close your file when done