Thread: put matrix from file into 2d dynamic array

  1. #1
    Registered User meriororen's Avatar
    Join Date
    Dec 2008
    Posts
    22

    put matrix from file into 2d dynamic array

    I am working on a program which reads a matrix of integer and inputting them into a 2-D dynamic array, and also finding its dimension. Here is what I made so far :

    Code:
            FILE *fp;
    	int **array, *subarr;
    	int xsize,ysize,i=0;
    	char n;
    	
    	fp = fopen("matrix.dat", "r");
    	if(fp == NULL){printf("error\n");return 0;}
    	
    
    	array = (int **)malloc(sizeof(int *));
    	array[0] = (int *)malloc(sizeof(int));
    	
    	xsize=ysize=0;
    	while(!feof(fp)){
    		fscanf(fp, "%d", &array[xsize][i]);
    		fscanf(fp, "%c", &n);
    		i++;
    		ysize++;
    		array[xsize] = realloc(array[xsize], (i+1) * sizeof(int));
    		if(n == '\n'){
    			xsize++;
    			i = 0;
    			array = realloc(array, (xsize+1) * sizeof(int *));
    			array[xsize] = (int *)malloc(sizeof(int));
    		}
    	}
    	xsize--;
    	ysize = (ysize-1) / xsize;
    	
    
    	for(i=0;i<xsize-1;i++){
    		for(j=0;j<ysize;j++){
    			printf("%d", array[i][j]);
    		}
    		printf("\n");
    	}
    here is the matrix.dat :

    Code:
    1 1 1 0 0 0 0 0
    0 1 1 1 1 0 0 0
    0 0 1 1 1 1 1 0
    0 0 0 1 1 1 1 1
    0 1 1 1 1 1 1 1
    0 1 1 1 1 0 1 1
    0 0 1 1 1 0 0 0
    0 0 1 1 1 1 0 0
    it gave me xsize = 6 and ysize = 10 instead of 8 X 8,

    It sometimes gave me the right value, but when I change the matrix, it become screwed up. If anyone can give me a hint to where I made mistake with the code, I would appreciate it. Thanks
    Last edited by meriororen; 06-08-2009 at 02:17 AM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Well, your allocation code is kind of overly complicated. What I would recommend is breaking it up into steps. You want to read a matrix, which can be broken down into reading in lines and converting one line into one row of the matrix.

    If you're unable to know the size of the matrix by the input right away (such as a line telling you what size to allocate), you could try determining this another way. For example, you could read the entire line with fgets, and examine how many spaces are in the line. That would give you an idea of how many elements you are dealing with per line. All in all, whatever you decide for implementation depends on what sizes you are expecting and what you plan on doing with them.

    For things actually wrong with your code, I spotted this right away.

  3. #3
    Registered User meriororen's Avatar
    Join Date
    Dec 2008
    Posts
    22
    Actually I wanted to do that, but I have to read the file more than once,
    I got stuck on returning the pointer of the file to the beginning after it is being read. I heard fclose() then fopen() again in some compiler don't return the pointer to the beginning of the file.

    any suggestion?

  4. #4
    Registered User meriororen's Avatar
    Join Date
    Dec 2008
    Posts
    22
    Hmmm, after I changed feof() with fscanf () == 1, It went well.

    Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  2. Weird errors.
    By Desolation in forum C++ Programming
    Replies: 20
    Last Post: 05-09-2007, 01:10 PM
  3. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. 2D dynamic array problem
    By scsullivan in forum C Programming
    Replies: 3
    Last Post: 12-30-2002, 10:02 PM