2014-03-29 1 views
1

파일에서 행렬을 읽고 할당 된 2D 배열에 저장하려고합니다.C 언어의 txt 파일에서 행렬 읽어 오기 및 할당 된 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 

아무도 도와 줄 수 있습니까?

+0

여기서'rows1'과'rows2'는 어디에서 초기화됩니까? – chrk

+0

전체 코드 또는 귀하의 독서 내용을 게시 할 수 있습니까 – Ruturaj

+0

코드를 편집했습니다 – user3476527

답변

1

% d 또는 % i를 사용하여 문제가 해결되었지만 문제가 해결되지 않으면 전체 코드가 도움이 될 것입니다.

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

코드 – user3476527

+0

을 편집 했으므로 % d을 (를) 사용하는 것이 효과가 없습니다. – Triad

+0

아니요. % d % 시도하여 작동했습니다 !! 그러면 곱셈 처리가 실패합니다! – user3476527

관련 문제