Writing to a file

This is a discussion on Writing to a file within the C Programming forums, part of the General Programming Boards category; Im currently writing a programme which involves reading from a txt file full of 1 0 1 0 0 0 ...

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

    Writing to a file

    Im currently writing a programme which involves reading from a
    txt file full of 1 0 1 0 0 0 binary code in a 100x100 table. I can
    read the file and apply the values to the matrix array and update
    it like i need to but when i write it back into the file it doesnt put
    in any of the 0's.
    Obviously this is a big problem!

    I think this is all the useful code:

    Code:
    in = fopen ( "matrix100x100.txt", "w" );
       if ( in == NULL ) 
       {
        printf ( "File open failure" );
       }
       fprintf ( in, "%d", matrix[r][c]);                
       fclose (in);    //closes file//
    can anyone tell me what im doing wrong please?
    I had thought of using %c or %f but i wasnt sure if
    that would allow me to add the values like i need
    to for bits.

    is fprintf the best way to do this?

    Basically...Please help!

    Thanks.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I think this is all the useful code
    Hmm. Try again, this time with code that we can compile and run immediately and the contents of the file so that we can play with it.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    10
    Well the full thing is this

    Code:
    #include <stdio.h>
    #include <clib.h>
    #include <stdlib.h>
    
    int main(void)
    {
      char repeat;
      int matrix[100][100],r,c,total;
      do
      {
      FILE *game;   
      printf("************************************\n*    Welcome to the Game of Life.  *\n************************************\n");
      if((game=fopen("matrix100x100.txt", "r")) != NULL)  //finds and reads matrix file//
    
    
            {
    
            printf ("File open\n\n");
    
            for ( r = 0 ; r < 100 ; r++ )    //controls rows//
                    {
                    for ( c = 0 ; c < 100 ; c++ )  //controls columns//
                            {
                            fscanf( game, "%d", &matrix[r][c] );//put values into Matrix from file//
                            printf(" %d", matrix[r][c]);       //print current Matrix Values//
                               
                            }
                            printf("\n"); //Give a new line at each row//
                    }
             }
       else printf ("File could not be opened\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("************************************\n*    Welcome to the Game of Life.  *\n************************************\n");
       printf ("Updating Matrix\n\n");
       for (r = 0 ; r < 100 ; r++ )    //increments rows//
                    {
                    for ( c = 0 ; c < 100 ; c++ )  //increments Columns//
                            { 
                             total = matrix[r-1][c-1]+matrix[r][c-1]+matrix[r+1][c-1]
                             +matrix[r-1][c]+matrix[r+1][c]+matrix[r-1][c+1]+matrix[r][c+1]
                             +matrix[r+1][c+1];  //sums neighbouring cells//
                             if (matrix[r][c]==0 && total==3 || matrix[r][c]==1 && total==2 || matrix[r][c]==1 && total==3)
                             {
                             matrix[r][c]=1; //sets value to alive if fitting rules//
                             }
                             else if (matrix[r][c]==1 && total>3 || matrix[r][c]==1 && total<2)
                                    {
                                     matrix[r][c]=0; //sets value to dead if fitting rules//
                                    }
                             printf(" %d", matrix[r][c]);  //prints new matrix values//     
                            }
                            printf("\n");
                    }             
       game = fopen ( "matrix100x100.txt", "w" );
       if ( game == NULL ) 
       {
        printf ( "File open failure" );
       }
       fprintf ( game, "%d", matrix[r][c]);                
       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//
       }
    And ive attatched the txt file. didnt want to bung everything up with too much code, but there you go!
    Thanks
    Attached Files Attached Files

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jan 2004
    Posts
    10
    we are friends, yes. I hope that wont be a problem.

    I dont really see why it should matter, we are both here
    asking for help on different aspects of c programming,
    which is what the board is for, and its not like im asking
    anyone to write code for me, just let me know what
    ive done wrong and if theres something i can do about
    it.

    Once again, thank you for your help.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > fprintf ( game, "%d", matrix[r][c]);

    This just prints one value to the file. A loop might help.

  7. #7
    Registered User
    Join Date
    Jan 2004
    Posts
    10
    THANK YOU SWOOPY!!!!!!
    Thats sorted it all now. I cant believe i didnt notice that!
    The programme now works perfectly so i am a very happy bunny!
    Thanks again & God Bless you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 09:47 AM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21