Thread: Need Help Quick .. weird

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    178

    Need Help Quick .. weird

    I am reading a file consisting of characters '*' and '$' and ' '.

    I verify the characters are placed properly into the array but reading them back out of the array produces mess.

    also, I can not get this code to stop reading garbage after the last character in the file.

    Anyone help please.


    Code:
    char dungeonDB[8][37];
    
    void readDungeonFile()
    {
    	FILE *inputFile;
    
    	inputFile = fopen ("C:\\Users\\ishareef\\Desktop\\dungeon.dat", "r");
    
    	if ( inputFile == NULL )
    		printf ("Unable to load dungeon.dat and generate the dungeon. Please check the file path.\n");\
    
    	 else
    	 {
    		 while (theChar != EOF)
    		 {
    			 for (i = 0; i < 8; i++)
    			 {
    				 for (j = 0; j < 37; j++ )
    				 {
    					 theChar = getc (inputFile);
    					 dungeonDB[i][j] = theChar;
    					 printf("%c", dungeonDB[i][j]);
    				 }
    			 }
    	   	 }
    
    	 }
    
    	 fclose (inputFile);
    
    	printf("\n");
    
    	 for (i = 0; i < 8; i++)
    	 {
    		 for (j = 0; j < 37; j++ )
    		 {
    			 theChar = dungeonDB[i][j];
    			 printf("%c", theChar);
    		 }
    	 }
    
    
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Imanuel View Post
    I verify the characters are placed properly into the array but reading them back out of the array produces mess.

    also, I can not get this code to stop reading garbage after the last character in the file.
    Not too sure about the first problem but the second one is because you test for EOF only after every 8 x 37 characters. What you should do is "invert" the arrangement by removing the while() loop and testing each char read in the innermost loop:

    Code:
    			 for (i = 0; i < 8; i++)
    			 {
    				 for (j = 0; j < 37; j++ )
    				 {
    					 theChar = getc (inputFile);
                                             if (theChar == EOF) break;
    					 dungeonDB[i][j] = theChar;
    					 printf("%c", dungeonDB[i][j]);
    				 }
                                     if (theChar == EOF) break;
    			 }
    	   	 }
    Notice that check needs to occur in the outer loop too, since "break" only escapes one loop.

    Make sense? Also make sure theChar is an int.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    178
    Hey thanks MK27. I will try this. I always feel like a noob with C programming even though I learned 3 semester of Java including data structures and C is close to that language.

    I bought a book for C and still don't understand the finer points like make theChar an int? But why when I need a char? Stuff like that man but yer the greatest.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    EOF is a negative value that is guaranteed to not be a value in a char. If you just use a char, it converts it to a char, so it is now no longer EOF. Put it this way: write a loop to write every value of a char to a file, now read them all in. You should be able to read them all in fine, while testing for EOF, and end up having read every actual character value the file contains. If you test for EOF as 'theChar' being a char instead of an int, then it's going to hit one of those values you wrote out earlier, and think it's really EOF, when it isn't.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird things with my linked list of queue
    By -EquinoX- in forum C Programming
    Replies: 3
    Last Post: 11-22-2008, 11:23 PM
  2. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 10:33 AM
  3. recursive quick sort - stack overflow
    By Micko in forum C Programming
    Replies: 9
    Last Post: 01-01-2005, 05:51 PM
  4. Questions on basic Quick Sort
    By Weng in forum C++ Programming
    Replies: 4
    Last Post: 12-16-2003, 10:06 AM
  5. Quick Sort Help
    By NavyBlue in forum C Programming
    Replies: 1
    Last Post: 03-02-2003, 10:34 PM