# Problems putting a text file into a matrix.

This is a discussion on Problems putting a text file into a matrix. within the C Programming forums, part of the General Programming Boards category; Im trying to create the "game of life" but thus far have found great problems trying to input a text ...

1. ## Problems putting a text file into a matrix.

Im trying to create the "game of life" but thus far have found great problems trying to input a text document into a matrix. Iv been trying to find a tutorial on the internet but none seem to answer the question, all i find is how to create an array or how to use a pointer.My code is as follows and is currently incomplete to run the program as I dont want the program written for me as its for my degree, I simply wish to know how to put the file "ref"(which comprises 1000, 1s or 0s) into the array "matrix"(10*10).
Code:
#include <stdio.h>
int main()
{
int row, column;
FILE *game;
int *reference_ptr;

game = fopen ("ref.txt", "r");

if (game == NULL)
{
printf ("File could not be opened\n");
}
else
{
printf ("File opened!  Closing it now...\n");
fclose (game);
}
reference_ptr=&game;
int matrix[10][10] =
{
*reference_ptr
};
}
I get the returned error:"0019 ERROR:An object of type '<ptr><ptr>_file' cannot be assigned to an object of type '<ptr>int'"

2. Problem 1) Fix your formatting
Problem 2)
I simply wish to know how to put the file "ref"(which comprises 10000, 1s or 0s) into the array "matrix"(10*10)
You can't stuff 10000 values into 100. Maybe matrix(100*100)

Problem 3) After you open the file you close it. You must read the file to get the values out of it before closing. Try getc(), fgets(), or read() to read the file, whichever works best for your file format.

3. >You can't stuff 10000 values into 100.
If the values are only 0 and 1, it is possible to stuff 10000 values into a 10x10 matrix. Consider one possible solution:
Code:
struct cell {
unsigned int a :32;
unsigned int b :32;
unsigned int c :32;
unsigned int d :4;
};

struct cell matrix[10][10];
Of course, this would certainly make working with the matrix more difficult as now you would have to twiddle bits fairly often. It would be must easier to have something like this instead:
Code:
struct cell {
unsigned int a :1;
};

struct cell matrix[100][100];
But that suggests space savings that aren't likely, so a 100x100 array of signed char would be both easier to work with and relatively space efficient. It all depends on what restrictions the OP is working under.

4. Originally posted by Prelude
>You can't stuff 10000 values into 100.
If the values are only 0 and 1, it is possible to stuff 10000 values into a 10x10 matrix.
Well, really, I know that. But since he can't even read a file, I was saving him a major migrain!

And I wouldn't even consider that a solution anyway. I hate migrains.

5. > int matrix[10][10] =
Assuming you mean't 100x100

Assumes your text file consists of 10000 numbers separated with whitespace (space,tab,newline)
Code:
for ( r = 0 ; r < 100 ; r++ ) {
for ( c = 0 ; c < 100 ; c++ ) {
fscanf( game, "%d", &matrix[r][c] );
}
}

6. its actually meant to be a file containing 100 characters, but iv been havin problems editting my original post. I wasnt sure where exactly to put wot u said but i have come up with this and get 2 errors for line 0026:"Subscript applied to the wrong type of object"
Code:
#include <stdio.h>
int main()
{
int row, column;
FILE *game;
int *reference_ptr;
int r,c;
int matrix;

game = fopen ("ref.txt", "r");

if (game == NULL)
{
printf ("File could not be opened\n");
}
else
{
printf ("File opened!  Closing it now...\n");
fclose (game);

for ( r = 0 ; r < 10 ; r++ )
for ( c = 0 ; c < 10 ; c++ )
fscanf( game, "%d", &matrix[r][c] );
};
}

7. Code:
#include <stdio.h>
int main(void)
{
int row, column;
FILE *game;
int r,c;
int matrix[10][10];

game = fopen ("ref.txt", "r");

if (game == NULL)
{
printf ("File could not be opened\n");
}
else
{
printf ("File opened!\n");

for ( r = 0 ; r < 10 ; r++ )
for ( c = 0 ; c < 10 ; c++ )
fscanf( game, "%d", &matrix[r][c] );

printf ("Closing it now...\n");
fclose (game);
}
}

8. thanks for anyone who helped, im now having problems using pointers could anyone show me an example or tutorial of how to use mutliple pointers accessing the values within an open file.This is what i have so far on the previous problem which now works perfectly but didnt know if it would affect the pointer so i posted it too.
Code:
#include<stdio.h>
int main(void)
{
FILE *game;
int r,c;
int matrix[10][10];
if((game=fopen("ref.txt", "r")) != NULL)

{

printf ("Game of Life blows!\n");

for ( r = 0 ; r < 10 ; r++ )
{
for ( c = 0 ; c < 10 ; c++ )
{
fscanf( game, "%d", &matrix[r][c] );
printf("%d", matrix[r][c]);
//to verify text is inputted//
}
printf("\n");
}
}

else printf ("File could not be opened\n");

printf ("Closing file\n");
fclose (game);
}

9. So what is your remaining question?

The latest code you posted looks pretty much like what others have posted, but doesn't contain anything from you showing where your next difficulty is.

> its actually meant to be a file containing 100 characters
You mean something like this?
Code:
00000
00100
01110
00100
00000
If it's like that, then %d is not the conversion you want to be using.

10. With the matrix iv made i need to add up all the numbers surrounding my given point(x,y) then do this for each value and save it to a new file.
eg.
(x-1,y-1),(x,y-1),(x+1,y-1)
(x-1,y),(x,y),(x+1,y)
(x-1,y+1)(x,y+1),(x+1,y+1)
Iv managed to do it without a pointer but iv tried to use it as below and i get the error:"An object of type 'int[100]' cannot be assigned to an object of type 'int'"
Iv used the following to give me a value for all the numbers surrounding (x,y):
Code:
        int total,var1,var2,var3,var4,var5,var6,var7,var8;
total=var1+var2+var3+var4+var5+var6+var7+var8;
var1=*(matrix+(x-1)+(y-1));
var2=*(matrix+(x)+(y-1));
var3=*(matrix+(x+1)+(y-1));
var4=*(matrix+(x-1)+(y));
var5=*(matrix+(x+1)+(y));
var6=*(matrix+(x-1)+(y+1));
var7=*(matrix+(x)+(y+1));
var8=*(matrix+(x+1)+(y+1));
printf("matrix:%d/n",total);

The problem is, you keep redeclaring matrix as something else each time around (usually incorrectly), and we can't keep up

12. Don't use pointers, use nested FOR loops:
Code:
total = o;
for (rx=-1; rx<2; rx++)
for (cx=-1; cx<2; cx++)
total += matrix[r+rx][c+cx];
r & c being the current location
be sure to test r+rx & c+cx for limits so you don't access values outside of matrix, like matrix[-1][10]

13. I can get the program to work without the pointers but i really wanna use them or i'll never get the hang of them. I seem to get the same problem over and over again:
0064 Error:=Requires an |value operand
0064 Error:=An object of type 'int' cannot b assigned to an object of type 'int'
0066 Error:=Only the integer value zero may b coerced to pointer type
0066 Error:=An object of type 'int' cannot b assigned to an object of type 'int'
0067 Error:=Requires an |value operand
0067 Error:=An object of type 'int' cannot b assigned to an object of type 'int[100]'
Code:
#include <stdio.h>
#include<clib.h>
#include <stdlib.h>
#define MAXCOL 100
#define MAXROW 100
int main(void)
{
FILE *game;
int r,c,total,N,score,x,y;
char repeat;

int matrix[100][100];
if((game=fopen("C:\\Documents and Settings\\lxh346\\Desktop\\ref.txt", "r")) != NULL)

{

printf ("Game of Life rules\n");

for ( r = 0 ; r < 100 ; r++ )
{
for ( c = 0 ; c < 100; c++)
{
{

{
fscanf( game, "%d", &matrix[r][c] );
printf("%d", matrix[r][c]);
}
printf("\n");
}

{
printf ("\nThis is the Original Generation
\n\nPress a Key to create Second Generation\n");
getch();       //Stops programme until user inputs a value//
system("cls"); //Clears Screen//
printf("    Welcome to the Game of Life.\n");
printf ("Updating Matrix\n\n");
{
{
for ( r = 0 ; r < MAXROW ; r++ )
{
for ( c = 0 ; c < MAXCOL; c++)
{
score = 0;
for(x=-1;x<=1;x++)
{
if( (r+x>=0) || (r+x<MAXCOL) )
for(y=-1;y<=1;y++)
if( (c+y>=0) || (c+y<MAXROW))
if(*(matrix + ((c+y)*MAXCOL) + (r+x) ) > 0)
{
score++;
}
}
}
}
}
if(score > 3)
line 64          if(*(matrix+(c*MAXCOL)+r)==0)
*(matrix+(c*MAXCOL)+r)=-1;
else
if(*(matrix + (c*MAXCOL) + r) == 1)
*(matrix + (c*MAXCOL) + r) = 2;

printf(" %d", matrix[r][c]);  //prints new matrix values//
}
printf("\n");
}
}
{
game = fopen ( "C:\\Documents and Settings\\lxh346\\Desktop\\ref.txt", "w" );
//open file with write access//
if ( game == NULL )
{
printf ( "File open failure" );   //incase of missing link//
}
}
fprintf ( game, "%d", matrix[r][c]);
//print values back into text file//
fclose (game);    //closes file//
printf ("\nDo you want to run it again? Y/N\n");    //option to re-run programme//
repeat=getch();
system ("cls"); //Clears Screen//
}
while (repeat=='y');     //if user types 'y', programme repeats//
}
else printf ("File could not be opened\n");
}

14. Why do you keep messing about with all those *'s for accessing array element?
I mean you can do it, but it sure makes life tricky

If you have
int matrix[100][100];

Then you can do
printf( "%d", matrix[0][0] ); // print an element
scanf( "%d", &matrix[0][0] ); // read an element from user
matrix[0][0] += 1; // add one

2nd point, your indentation is a mess.

15. Originally posted by warny_maelstrom
I can get the program to work without the pointers but i really wanna use them or i'll never get the hang of them.
Then come up with a program that would be better with pointers. Matrix manipulation the way you want to do it will be hard, time consuming, confusing, and other bad adjectives. You will learn to hate pointers using this program.

Try something like writing your own strcpy(), strcmp() functions to start understanding pointers. At the beginning, string manipulation is an easy way to understand how pointers work.

Page 1 of 2 12 Last