gpt4 book ai didi

c - 用C语言从txt文件中读取矩阵并将其存储在分配的二维数组中

转载 作者:太空宇宙 更新时间:2023-11-04 03:48:19 30 4
gpt4 key购买 nike

我正在尝试从文件中读取矩阵并将其存储在分配的二维数组中。

但它只是读取前 3 或 4 个数字,其余的被读取为垃圾。

这是我的代码:

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


int main()
{
FILE *inputFile1;
FILE *inputFile2;
FILE *outputFile1;
int i,j,k;

int rows1,cols1,rows2,cols2;

char fileName[100];
bool fileFound=false;

bool multiplcation = true;

int sum=0;

do
{
printf("Enter File name of matrix #1 - with extention - : ");
scanf("%[^\n]%*c",fileName);
inputFile1 = fopen(fileName,"r");
if(!inputFile1)
{
printf("File Not Found!!\n");
fileFound=true;
}
else
fileFound=false;

}while(fileFound);


do
{
printf("Enter File name of matrix #2 - with extention - : ");
scanf("%[^\n]%*c",fileName);
inputFile2 = fopen(fileName,"r");
if(!inputFile2)
{
printf("File Not Found!!\n");
fileFound=true;
}
else
fileFound=false;

}while(fileFound);

fscanf(inputFile1,"%i",&rows1);
fscanf(inputFile1,"%i",&cols1);
fscanf(inputFile2,"%i",&rows2);
fscanf(inputFile2,"%i",&cols2);

printf("\n\nRows1 = %d",rows1);
printf("\nCols1 = %d",cols1);
printf("\n\nRows2 = %d",rows2);
printf("\nCols2 = %d",cols2);


if(cols1!=rows2)
multiplcation=false;
else
multiplcation=true;

if(multiplcation==false)
{
printf("Cant multiply those two matrices \n");
fclose(inputFile1);
fclose(inputFile2);
return 0;
}

else
{
//allocate Matrcies

int **mat1 = (int **)malloc(rows1 * sizeof(int*));
for(i = 0; i < rows1; i++)
mat1[i] = (int *)malloc(cols1 * sizeof(int));

i=0;

int **mat2 = (int **)malloc(rows2 * sizeof(int*));
for(i = 0; i < rows2; i++)
mat2[i] = (int *)malloc(cols2 * sizeof(int));

i=0;

int **mat3 = (int **)malloc(rows1 * sizeof(int*));
for(i = 0; i < rows1; i++)
mat3[i] = (int *)malloc(cols2 * sizeof(int));

i=0;


while(!feof(inputFile1))
{
for(i=0;i<rows1;i++)
{
for(j=0;j<cols1;j++)
fscanf(inputFile1,"%d%*[^\n]%*c",&mat1[i][j]);
}
}

i=0;
j=0;

while(!feof(inputFile2))
{
for(i=0;i<rows2;i++)
{
for(j=0;j<cols2;j++)
fscanf(inputFile2,"%d%*[^\n]%*c",&mat2[i][j]);
}
}

/////////////////////////
i=0;
j=0;
printf("\n\n");
//print matrix 1
for(i=0;i<rows1;i++)
{
for(j=0;j<cols1;j++)
printf("%d\t",mat1[i][j]);

printf("\n");
}
////////////////////////////
i=0;
k=0;
printf("\n\n");
//print matrix 2
for(i=0;i<rows2;i++)
{
for(j=0;j<cols2;j++)
printf("%d\t",mat2[i][j]);

printf("\n");
}
/////////////////////////
i=0;
j=0;
//multiplication stetments
for(i=0;i<rows1;i++)
{
for(j=0;j<cols2;j++)
{
sum=0;
for(k=0;k<rows2;k++)
sum+=mat1[i][k]*mat2[k][j];
}
mat3[i][j]=sum;
}

i=0;
j=0;
k=0;

//print multiplication result
printf("\n\nResult = \n\n");

for(i=0;i<rows1;i++)
{
for(j=0;j<cols2;j++)
printf("%d\t",mat3[i][j]);

printf("\n");
}


fclose(inputFile1);
fclose(inputFile2);
}

}

我的第一个文件是这样的:

    4 3
1 2 3
2 3 1
3 1 2
1 3 2

我的第二个文件是这样的:

    3 4
1 2 3 1
2 1 2 3
3 1 3 2

谁能帮帮我?

最佳答案

仅使用 %d 或 %i 就解决了我的问题,但如果这不能解决您的问题,我们将需要您的完整代码来帮助您。

fscanf(inputFile1, "%d", &mat1[i][j]);

关于c - 用C语言从txt文件中读取矩阵并将其存储在分配的二维数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22735852/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com