Thread: Reading maps from a .txt file?

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    99

    Question Reading maps from a .txt file?

    Ok, I know somebody has done this in a game I played recently, called 'Light' (sorry, I forgot the person's name but it was great!). And although it wasn't console based I am sure this is possible in console. What I need to know is how would I read from a .txt file and insert it into a 2d array? I haven't done much in File I/O, but would I use a 'For Loop' to read each character then assign it to the correct position? And how would I go about doing this? Thanks in advance! You guys are great!

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    43
    I did something like that recently and I had something like this for inserting chars into 2d arrays:

    int cap1 = whatever number;
    int cap2 = whatever number;

    int one[cap1][cap2];
    char val;

    //need to open the text file, make sure you include <fstream>
    ifstream infile("myfile");

    //basically its one[i][j], i for rows and j for columns

    for (int i = 0; i<cap1; i++){
    for (int j = 0; j<cap2; j++){

    infile >>val;
    one[i][j] = val;
    }}

    this should go 00, 01, 02....and so on...

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    99

    thankyou

    ok thx!

    Edit: I can't beleive I couldn't figure that out... lol. I was thinking too hard, and the logic got me all muddled up! lol, thx again!
    Last edited by Da-Spit; 07-02-2002 at 11:56 PM.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    99

    Grrr

    Ok, another problem! lol. Because I need to read integers of multiple digits, I've decided to use commas to seperate them. I have two variables that I use: temp, and val. I read in temp then check if it is a comma or not, if it is not then val = temp, and continues with the rest of the program... If it does equal a comma then val is not set to = temp, it then continues reading... BUT, because I use temp to check for both numbers and characters, I am not sure what data type to declare it as. Shoud I create a third variable just to check for commas? And one more thing, I want it to keep reading unless it sees a comma, if this is my text file:

    1,44,2,7

    At the moment it would:
    1) read the 1 and assign it to grid[0][0]
    2) read the comma and ignore it
    3) read the 4 and assign it to grid[0][1]
    4) read the 4 and assign it to grid[0][2]
    etc...

    but I don't want it to read the two 4's seperately, I want it to read is as 44 and then enter it into grid[0][1]. I hope you understand what my problem is. I think I might be able to figure out the 1st problem (although some advice would be nice), but I really have no idea how to solve the second problem! Please reply, Thankyou so much!!!!!!
    Last edited by Da-Spit; 07-03-2002 at 12:57 AM.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    305
    i dont think that would work. you declare an array with a variable.

    i think your problem is that youre inputting chars not integers. instead, try inputting integers, then eating the comma, and so forth. either that, or get rid of the commas and just use spaces and input integers.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    99
    Ok, I decided to go with using spaces... Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM