Thread: Problems putting a text file into a matrix.

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    13

    Arrow 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'"
    Last edited by warny_maelstrom; 01-20-2004 at 04:38 PM.

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    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.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.
    My best code is written with the delete key.

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    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.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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] );
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jan 2004
    Posts
    13
    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] );  
      };      
      }
    Last edited by warny_maelstrom; 01-20-2004 at 05:40 PM.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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);
      }
    }
    Last edited by swoopy; 01-20-2004 at 05:38 PM.

  8. #8
    Registered User
    Join Date
    Jan 2004
    Posts
    13
    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); 
    }
    Last edited by warny_maelstrom; 01-21-2004 at 07:21 AM.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Jan 2004
    Posts
    13
    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);
    Last edited by warny_maelstrom; 01-21-2004 at 01:59 PM.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Post your whole program
    The problem is, you keep redeclaring matrix as something else each time around (usually incorrectly), and we can't keep up
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    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]
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  13. #13
    Registered User
    Join Date
    Jan 2004
    Posts
    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");  
    }
    Last edited by warny_maelstrom; 01-22-2004 at 06:54 AM.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    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.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Replies: 19
    Last Post: 05-30-2007, 05:47 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM