printing to an array from file

This is a discussion on printing to an array from file within the C Programming forums, part of the General Programming Boards category; I'm having some difficulty (no big surprise) printing the values in a file into a 20x20 array. At the moment ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    2

    printing to an array from file

    I'm having some difficulty (no big surprise) printing the values in a file into a 20x20 array. At the moment the file is just made up of a whole heap of 1s set out in the format of a square array. Have I done something majorly wrong in this code? Particularly the use of !feof and fgetc.

    'map' is the file pointer.

    Code:
      if((map = fopen("map1.txt", "r")) == NULL){
            printf("error opening file.");
        } 
    
        while(!feof(map)){
            for(x=0; x < 20; x++){            
                for(y=0; y < 20; y++){                
                    array[x][y] = fgetc(map);
                    y++;
                }
                x++;
            }
        }
    Your help is really appreciated

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > while(!feof(map)){

    Let me beat Salem to the punch...

    'feof' is a Bad Thing(TM).

    Code:
    for(x=0; x < 20; x++){            
                for(y=0; y < 20; y++){                
                    array[x][y] = fgetc(map);
                    y++;
                }
                x++;
    But there is your problem. You are incrementing 'x' and 'y' twice. Stopit. Remove the single 'x++;' and 'y++;' lines.

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

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    2
    I made the change, now I'm getting an infinite loop. Before I wasn't but had an array full of garbage instead of the 1s that were in the file. Is this because of the fgetc thing that I'm using?

    What's a better thing to use in the place !feof ?

    Code:
        if((map = fopen("map1.txt", "r")) == NULL){
            printf("error opening file.");
        }
    
        while(!feof(map)){
            for(x=0; x < 20; x++){
                for(y=0; y < 20; y++){
                    array[x][y] = fgetc(map);
                }
            }
        }

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Your still using feof() ? Stop stop stop it...
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	char array[20][20];
    	int c;
    	FILE *fp;
    	
    	if ((fp = fopen("test.txt", "r")) == NULL)
    	{
    		perror("test.txt");
    		return (1);
    	}
    	
    	while ((c = fgetc(fp)) != EOF)
    	{
    		if (c != '\n')
    		{
    			putchar (c);
    			// add c to the array or whatever you want
    		}
    	}
    	
    	fclose(fp);
    	return (0);
    }
    This is just an example of how to read chars from a file. Note that c is defined as an int not a char. This is done to allow us to use the return code from fgetc() in a test against EOF.

    Also note, fgetc() will give you the newline characters, so you have to deal with them specially.

    Lastly, if you are looking to store the numbers from the file as numbers in the program,
    -EG 7 in file being equivalent to int i = 7; in the program
    you'll need to do some conversion, as 7 in the file is an ASCII character 7, not a numeric 7. Research atoi() for this.

    Happy coding......
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    My diaper's full....... stevey's Avatar
    Join Date
    Nov 2001
    Posts
    759
    whats wrong with feof ????

    or shall i do a search ?????
    Steve

  6. #6
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212
    Stevey
    The problem, it reads the last line twice in a while loop.

    kwigibo

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    feof() only knows you're at the end of the file when you have performed a read and failed. In the first sample code (using feof()), the loop always stores values in the array after doing a read. It will do this even we it gets EOF. It then checks feof(), but by then it's too late.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 08:51 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM

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