Reading in an array of text from a file?

This is a discussion on Reading in an array of text from a file? within the C++ Programming forums, part of the General Programming Boards category; Well, I've got single lines down, so I'm not a total failure here. However, I'm trying to read a 10x10 ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    106

    Post Reading in an array of text from a file?

    Well, I've got single lines down, so I'm not a total failure here. However, I'm trying to read a 10x10 array of characters from a textfile into a 10x10 array of characters in a... char map[10][10].

    Someone suggested using getline, 10 times, in a loop.

    Code:
    int main()
    {
         ifstream mapFile;
         mapFile.open("map/map01.txt");            
         mapFile.getline(&map[0][0],10);
         mapFile.close();
         cout << map[1][1];
         int hi;
         cin >> hi;
         return 0;
    }
    So, when the 10xtimes in a loop thing failed, I took the loop bit out and just tried reading in the first line of text.

    Now, my understanding of the syntax is this: getline reads from mapFile, and puts the text into map, starting at *map[0][0], and reads in for... 10 characters in the text file. From my understanding, this should put the first line of the text file into positions [0][0] to [10][0] in char map.

    Obviously my understanding is wrong, because I'm making this post. More specifically, I get blank output from cout << map[1][1]. Which, from my understanding, should display the character in the array at position [1][1]. I tested this by include map[1][1] = 'a' right above the cout bit.

    Er, yes, so I guess I'm wondering what I need to plug into getline to... get it properly reading my lines, or if that's the entirely wrong approach.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    what's wrong with reading it in character-by-character?

    did nested for loops go out of syle or something?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    106
    Well, on the subject of reading it in character by character, I'm... not really sure how to do that either. I know it involves nested for loops with increasing x and y values for a char[x][y] sort of set up, but I'm not sure how to get x and y there to point at proper points in the loaded file.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Call mapFile.getline(map[0],10), since map[0] is a character array just like if you had a single character array that you were using with getline. If you put that call in a loop, then you would use map[i], basically looping through each character array in the map array.

    You should look into using the C++ string class instead of character arrays, though. Also, the C++ vector class is a better idea than a plain C style array.

    Finally, the reason map[1][1] doesn't work in the code above is that you read into the character array at map[0], then you attempt to output the string at map[1]. Change that to map[0] and you will get your string. Change it to map[0][0] and you will get the first character of map[0]. The two dimensional array syntax means that map[0][4] is the 5th character of the first string, and map[4][0] is the first character of the 5th string.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    15
    >>I know it involves nested for loops with increasing x and y values for a char[x][y] sort of set up, but I'm not sure how to get x and y there to point at proper points in the loaded file.

    call me outdated and complex, but here's how they taught me back when i was in tenth grade. i know they lied to me about complexity, seing that nested for loops are n*n complexity ..here, tho' it wouldn't be that bad... it's just 10*10 complexity by my counts
    Code:
    ...
    fstream f("map/map01.txt", ios::in);//that's for opening the file 
     for(int i=0; i<10;i++)
       for(int j=0;1<10;j++)
         f>>map[i][j];//and reading it into a "map", [10][10]array
    ...and then when you want to print it on screen you use the same kind of nested loops just that you have the "cout<<a[i][j]<<" "; the blank is optional, depending on your level of tidyness

    ----
    sorry, it was ninth

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    106
    Hm. Thought I tried doing that. Maybe I was opening it in binary mode or something. Which would explain a few things...

    Granted, I'm not really sure this is the optimal method for using with a map engine anyway, but I guess I got a be stuck in my bonnet. Will try :P Thanks.

    ---edit---

    Unfortunately, this did not work.
    Fortunately, it was just because I forgot my fstream source wasn't anywhere NEAR where I had map00.txt. Hence, after I moved it around, all worked.

    One thing I still don't get, though.

    Code:
     for(int i=0; i<10;i++)
       for(int j=0;1<10;j++)
         f>>map[i][j];//and reading it into a "map", [10][10]array
    j is the y-axis in the 10x10 map array, isn't it? I thought it read files in from left to right. Wouldn'tthat require it to go top to bottom?

    Or am I wrong about x/y correlation?
    Last edited by suzakugaiden; 01-04-2006 at 02:09 PM.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    j is the x axis. If your file looks like this:
    Code:
    0123456789
    abcdefghij
    klmnopqrst
    0123456789
    abcdefghij
    klmnopqrst
    0123456789
    abcdefghij
    klmnopqrst
    uvwxyzuvwx
    Then map[0][0] will be '0' and map[0][1] will be '1' and map[0][9] will be '9' and map[1][0] will be 'a' and map[1][9] will be 'j' and map[9][9] will be 'x'.

    Also, map[0] will be "0123456789" (with no terminating null character) and map[1] will be "abcdefghij" (again without a terminating null).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a list of ints from file into an array
    By mesmer in forum C Programming
    Replies: 1
    Last Post: 11-10-2008, 05:45 AM
  2. reading from text file
    By jamez in forum C Programming
    Replies: 3
    Last Post: 11-30-2005, 06:13 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Reading Characters from file into multi-dimensional array
    By damonbrinkley in forum C Programming
    Replies: 9
    Last Post: 02-24-2005, 12:31 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

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