6 Replies - 12702 Views - Last Post: 25 February 2011 - 09:01 AM Rate Topic: -----

#1 Guest_Nayefc*


Reputation:

Reading a matrix from a file into a multidimensional Array in C

Posted 25 February 2011 - 08:14 AM

Hello,

So I've searched everywhere and for some reason my code isn't working. Here is what I want to do..
I am generating a .txt file with a matrix. The matrix is different each time, depending on input. So here is an example of one of the outputs that I can possibly have:
7 8 9
6 1 2
5 4 3

I malloced a multidimensional array that has the same size as the matrix in the file and initialized its contents to -1 as follows:

	int **array;
	array = malloc(n * sizeof(int *));

	if(array == NULL) {
		printf("Out of memory\n");
		exit(1);
	}

	for(int i = 0; i < n; i++) {
		array[i] = malloc(n * sizeof(int));
		if(array[i] == NULL) {
			printf("Out of memory\n");
			exit(1);
		}
	}

	// Initialize matrix content to -1
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			array[i][j] = -1;
		}
	}




Now I want to read in the text file matrix and put each number in the corresponding position in the array. I've done all sorts of stuff, searched everywhere, used all the books I have and it is just not working.. Here are examples of what I tried to do:

When printing the array after doing the following, its contents are still -1:
	int x;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			x = fscanf(pFile, "%d", &array[i][j]);
		}
	}



I've also tried to do a while loop and check for end-of-file, while (fscan...) != EOF and that did not work either.

Most posts online show methods on reading a matrix when you know the size of the matrix. However, my size of matrix is determined upon runtime. And all the methods I saw online are just not working..


I appreciate any help.


Thanks,

Is This A Good Question/Topic? 0

Replies To: Reading a matrix from a file into a multidimensional Array in C

#2 Guest_Nayefc*


Reputation:

Re: Reading a matrix from a file into a multidimensional Array in C

Posted 25 February 2011 - 08:22 AM

Sorry I forgot to add that the following also does not work and prints out the array as -1:
	for (int i = 0; i < n; i++) {
	  for (int j = 0; j < n; j++) {
		  fscanf(pFile, "%d \n", &array[i][j]);
	  }
	}


This post has been edited by JackOfAllTrades: 25 February 2011 - 08:40 AM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? 0

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5929
  • View blog
  • Posts: 12,851
  • Joined: 16-October 07

Re: Reading a matrix from a file into a multidimensional Array in C

Posted 25 February 2011 - 08:29 AM

Check to make sure you're reading what you think you are from the file:
for (int i = 0; i < n; i++) {
	for (int j = 0; j < n; j++) {
		int n;
		fscanf(pFile, "%d", &n);
		printf("(%d,%d) = %d\n", i, j, n);
		array[i][j] = n;
	}
}


This post has been edited by baavgai: 25 February 2011 - 08:29 AM

Was This Post Helpful? 1
  • +
  • -

#4 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: Reading a matrix from a file into a multidimensional Array in C

Posted 25 February 2011 - 08:31 AM

Have you checked the file opens correctly?
ie
FILE *pFile = fopen("nums.txt","r");
if(pFile==NULL)
{
   printf("file could not be opened");
   exit(1)
}



It helps if you post the whole code.
Was This Post Helpful? 0
  • +
  • -

#5 Nayefc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-February 11

Re: Reading a matrix from a file into a multidimensional Array in C

Posted 25 February 2011 - 08:39 AM

View Postbaavgai, on 25 February 2011 - 08:29 AM, said:

Check to make sure you're reading what you think you are from the file:
for (int i = 0; i < n; i++) {
	for (int j = 0; j < n; j++) {
		int n;
		fscanf(pFile, "%d", &n);
		printf("(%d,%d) = %d\n", i, j, n);
		array[i][j] = n;
	}
}



Hmm.. Interesting. When the matrix size is 5, the spiral.txt is:
21 22 23 24 25
20 7 8 9 10
19 6 1 2 11
18 5 4 3 12
17 16 15 14 13

When using your loop, the output is as follows. Where is it reading zeros from?
(0,0) = 0
(0,1) = 0
(0,2) = 0
(0,3) = 0
(0,4) = 0
(1,0) = 0
(1,1) = 0
(1,2) = 0
(1,3) = 0
(1,4) = 0
(2,0) = 0
(2,1) = 0
(2,2) = 0
(2,3) = 0
(2,4) = 0
(3,0) = 0
(3,1) = 0
(3,2) = 0
(3,3) = 0
(3,4) = 0
(4,0) = 0
(4,1) = 0
(4,2) = 0
(4,3) = 0
(4,4) = 0


View Postsimeesta, on 25 February 2011 - 08:31 AM, said:

Have you checked the file opens correctly?
ie
FILE *pFile = fopen("nums.txt","r");
if(pFile==NULL)
{
   printf("file could not be opened");
   exit(1)
}



It helps if you post the whole code.



I am using the same file for input and output by tagging it with w+ and yes I am checking if it opens correctly (it obviously is because its writing the matrix to the file.

Here is my driver.c

#include <stdio.h>
#include <stdlib.h>

int main() {

	/*
	 * Initialization and Allocation
	 */
	int n;
	scanf("%d", &n);

	// Allocate Matrix
	int **array;
	array = malloc(n * sizeof(int *));

	if(array == NULL) {
		printf("Out of memory\n");
		exit(1);
	}

	for(int i = 0; i < n; i++) {
		array[i] = malloc(n * sizeof(int));
		if(array[i] == NULL) {
			printf("Out of memory\n");
			exit(1);
		}
	}

	// Initialize matrix content to -1
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			array[i][j] = -1;
		}
	}

	// Open file to write spiral to
	FILE *pFile = fopen("spiral.txt", "w+");
	if (pFile == NULL) {
		printf("Error.");
		exit(1);
	}

	// Generate Spiral // FindSpiral()  IS WHERE THE MATRIX IS GENERATED AND WRITTEN TO THE FILE IS "%4d" printf() statements
	for (int i = 0; i < n; i++) {
		FindSpiral(n, i, pFile);
		fprintf(pFile, "\n");
	}

	for (int i = 0; i < n; i++) {
	  for (int j = 0; j < n; j++) {
		  int x;
		  fscanf(pFile, "%d", &x);
		  printf("(%d,%d) = %d\n", i, j, x);
		  array[i][j] = x;
	  }
	}


	// Deallocate spiral array
	for (int i = 0; i < n; i++) {
		free(array[i]);
	}
	free(array);
	array = NULL;

	// Close files
	fclose(pFile);

	return 0;
}





So for FindSpiral(), when n = 5, the output in the spiral.txt is:
21 22 23 24 25
20 7 8 9 10
19 6 1 2 11
18 5 4 3 12
17 16 15 14 13
Was This Post Helpful? 0
  • +
  • -

#6 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: Reading a matrix from a file into a multidimensional Array in C

Posted 25 February 2011 - 08:58 AM

After writing to the file - fscanf will read from the end of the file. To read from the start of the file you could close and reopen it (in mode "r"), or use fseek, to go to the beginning again.

This post has been edited by simeesta: 25 February 2011 - 08:58 AM

Was This Post Helpful? 2
  • +
  • -

#7 Nayefc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-February 11

Re: Reading a matrix from a file into a multidimensional Array in C

Posted 25 February 2011 - 09:01 AM

View Postsimeesta, on 25 February 2011 - 08:58 AM, said:

After writing to the file - fscanf will read from the end of the file. To read from the start of the file you could close and reopen it (in mode "r"), or use fseek, to go to the beginning again.



Oh right!!!

I just used rewind(pFile) and it worked!


Thank you very much..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1