从C语言中的txt文件读取矩阵并将其存储在分配的2D数组中

时间:2014-03-29 19:32:47

标签: c file matrix multidimensional-array dynamic-allocation

我试图从文件中读取矩阵并将其存储在已分配的2D数组中。

但它只是阅读前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

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

只是使用%d或%i为我解决了问题,但如果这不能解决您的问题,我们将需要您的完整代码才能为您提供帮助。

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