Reading files number by number, also getw()?

This is a discussion on Reading files number by number, also getw()? within the C Programming forums, part of the General Programming Boards category; Okay... I'm trying to read a map file in integer by integer ignoring commas and newlines and this is the ...

  1. #1
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    Reading files number by number, also getw()?

    Okay... I'm trying to read a map file in integer by integer ignoring commas and newlines and this is the code I've written so far:

    Code:
    void LoadWorld (const char* fn)
    {
    	FILE *f;
    	if ((f = fopen(fn, "r")) == NULL)
    		return; 
    
    	for (int y = 0; y < MAP_H; ++y) {
    		for (int x = 0; x < MAP_W; ) {
    			int c = getw(f);
    
    			if (feof(f))
    				break;
    			
    			if (char(c) != '\n' &&
    				char(c) != ',') {
    				MAP[y][x] = c;
    				++x;
    			}
    		}
    	}
    
    	fclose(f);
    }
    Now for some strange (or completely normal, understandable) reason, this works but has some garbled blocks left over. My map file is a text file formatted like this:

    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    1,1,1,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,2,

    etc...

    MAP_W and MAP_H are the right numbers. What am I overlooking in this that makes it not read it all (it just reads the second half it seems, the rest of the map is left as 0s)? Also what would be a good way to do this without using getw(), as I understand it's not good for portable code. (I tried using fscanf(f,"%d,",&c) but that doesn't work at all)

    Thank you for any help

  2. #2
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    _

    int numbers, not ascii values.

    I see what you're saying though...
    so I tried typcasting to unsigned char then subtracting 48. It still loads the map incorrectly, although atleast now it actually loads something instead of 0s. I'm not sure if the problems now in how I get the data or how I put it into my map...

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    I think..

    I think is possible to do what you want with fread(), not?

    call fread with nelem = 1 and size = sizeof(int) and it does the exact same thing.

    notice this is a binary integer, not an ascii integer.

  4. #4
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    _

    Ok I tried this:

    Code:
    // byte is unsigned char
    
    fread(MAP,sizeof(byte),MAP_W*MAP_H, f);
    but it doesn't work. Is there anything special I should be doing to fill my 2D array? I really don't want to make my array 1D, or is this just not possible with 2D arrays?

    Also how should I format my data so fread will read each number seperately, will putting spaces between the numbers work?

    or will fread just not work with this methodology... =)

    thanks!

  5. #5
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ...lol

    I guess adding that == 1 changes everything... lol. It works now, thanks a lot Salem!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about reading files in
    By smd in forum C++ Programming
    Replies: 11
    Last Post: 08-25-2003, 07:40 PM
  2. Replies: 0
    Last Post: 07-12-2002, 01:40 PM
  3. Having some trouble with reading from files
    By bluebob in forum C Programming
    Replies: 19
    Last Post: 04-18-2002, 05:29 AM
  4. Reading Files off the internet
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 03-24-2002, 01:18 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM

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