【问题标题】:Reading matrix from txt file in C Language and store it in allocated 2D array从 C 语言中的 txt 文件中读取矩阵并将其存储在分配的二维数组中
【发布时间】:2014-05-09 06:34:13
【问题描述】:

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

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

谁能帮帮我?

【问题讨论】:

  • rows1rows2 初始化在哪里?
  • 你能发布完整的代码或你的阅读内容

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


【解决方案1】:

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

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

【讨论】:

  • 不,但我尝试了 %d% 并且成功了!!那么乘法过程失败!
  • 所以应用程序打印出结果,但结果不正确,还是乘法时程序崩溃?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-23
  • 1970-01-01
  • 2019-07-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多