reading from text file to two dimensional array -


i try read text file , pass integers 2 dimensional array 1 one. when print tried pass, weird outputs. problem? example if text is: 0 1 1 1 0 1 1 0 1 1 1 1

i this:

index=-2 index=1967626458 index=1967694074 index=207568 index=207320 index=2686776 index=1967693597 index=0 index=0 index=2686832 index=236 index=228 index=3

here code:

    #include<stdio.h>      int main()     {      file *input;     //read file!      if((input = fopen("abc.txt","r"))==null){         printf("error in reading file !\n");         return 0;     }      int c = 4;     int r = 3;     int m[3][4];     int x=0;     int y=0;     int c;     //array of sorted list!     while(!feof(input)){         if(!feof(input)){              fscanf( input, "%d",&c);            m[x][y]=c;             y++;             if(y==c){              x++;             y=0;               }     printf("index=%d \n",m[x][y]);          }      }        system("pause");     } 

the printout wrong because you're changing values of x , y between when set variable , when try print it. need move printf() before part increment x , y, after assign array.

as stands right now, assign array, print next, yet unassigned, value. it's whatever value happens in memory, such -2 or 1967626458.


Comments