Thread: Still having problems with ifstream...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    8

    Still having problems with ifstream...

    I've been messing with this for a few hours and I just can't seem to get what I want. I've been over the documents on this for a while and I can't see what I need.

    What I want: I've got a text file that has data in it(20x15): and I want to read in the data (as an integer). However, the data is read in as ASCII and my 0's are 48's, 1's are 49's and the 2 is 50.

    001 001 001 001 001 001 001 001 001 001 001 001 001 001 001 001 001 001 001 001
    001 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
    001 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
    001 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
    001 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
    001 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
    001 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
    001 000 000 000 000 000 000 000 000 000 002 000 000 000 000 000 000 000 000 000
    001 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
    001 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
    001 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
    001 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
    001 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
    001 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
    001 001 001 001 001 001 001 001 001 001 001 001 001 001 001 001 001 001 001 001

    Here is my code that I'm working with.

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main(){
        
        int worldX=20;
        int worldY=15;
        int tileID[20][15];
    
        ifstream mapInput("worldmapsmall.txt");
        
        for(int y=0; y<worldY; y++){
            for(int x=0; x<worldX; x++){
                tileID[x][y] = mapInput.get();
                
                //if there is a new line character (ASCII value 10) get the next
                //digit and place it in tileID[x][y]
                if (tileID[x][y] == 10) {
                    tileID[x][y] = mapInput.get();
                    cout << endl;
                    cout << tileID[x][y];
                }
                else{
                    cout << tileID[x][y];
                } 
            }
        } 
        mapInput.close();
    }
    What do I need to do to pull each block of 3 and store that set as 1 integer in the location tileID[x][y]?

    The set of data that the above works with looks like this:

    11111111111111111111
    10000000000000000001
    10000000000000000001
    10000000000000000001
    10000000000000000001
    10000000000000000001
    10000000020000000001
    10000000000000000001
    10000000000000000001
    10000000000000000001
    10000000000000000001
    10000000000000000001
    10000000000000000001
    10000000000000000001
    11111111111111111111

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    How about:
    Code:
    for(int y = 0; y < height; ++y)
      for(int x = 0; x < width; ++x)
        file >> tileID[x][y];
    Since it's just numeric input, you can let the istream's overloaded operator>> do all the nasty conversion etc. for you.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    8
    Quote Originally Posted by Hunter2
    How about:
    Code:
    for(int y = 0; y < height; ++y)
      for(int x = 0; x < width; ++x)
        file >> tileID[x][y];
    Since it's just numeric input, you can let the istream's overloaded operator>> do all the nasty conversion etc. for you.
    When I do that I get this:

    2089929714
    0
    0
    2089952188
    131728
    0
    2147156510
    14
    ...

    Code:
    ifstream mapInput("worldmapsmall.txt");
        
    for(int y=0; y<worldY; y++){
        for(int x=0; x<worldX; x++){
            mapInput >> tileID[x][y];
            cout << tileID[x][y] << endl;
        }

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    8
    I got it... thanks for the help guys.

    I didn't think to break my numbers apart and was getting integer overflow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  2. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  3. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  4. Read problems
    By xddxogm3 in forum C++ Programming
    Replies: 10
    Last Post: 10-05-2003, 12:17 AM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM