开发者

Reading from text file into an array

I'm trying to read a textfile that I've edited with Vim into an array.

The textfile is 30*50 and is composed of single digit numbers. I've been going crazy trying to get it to work, but I think I'm having issues due to newline characters. Here's what I've been using:

Map::Map(char* filename)
{
grid[30][50] = (0);
string line;
ifstream m_file(filename);
if (m_file.is_open())
        {
                while(m_file.good())
                 {
                        for (int i = 0; i < 30; i++)
                        {
                        getline(m_file,line);
                                 for (int k = 0; k < 50; k++)
                                {
                                int tnum = atoi(line.c_str());
                                grid[i][k] = tnum;
                                }
                        }
                }
                m_file.close();
        }
};

grid is defined in the header file as int grid[30][50].

The code I use to print is as follows:

void display_room(int开发者_StackOverflow中文版 trid[30][50])
{
        for (int i = 0; i < 30; i++)
        {
                for (int k = 0; k < 50; k++)
                {
                        mvprintw(i,k,"%d",trid[i][k]);
                };
        };
};

after calling Map sMap = Map("testmap");

I'm simply trying to capture the single digit numbers into an array, and reprint that array (using curses). Currently, it reads the testmap file, and prints all zeros, no matter what is in the testmap file.


If I understand Your problem: Your parsing sets the value from the entire line where only a digit should be...

int tnum = atoi(line.c_str());
grid[i][k] = tnum;

Translating the digit (ASCII to an int/byte/... can be done in this way:

grid[i][k] = line[k] - '0';

(Perhaps some casting is needed.)


In the inner loop, you're calling atoi with the full content of the line each time. As the line is 50 character long, atoi cannot convert it to an int (the largest representable value by an int is 2147483647, and your number is probably larger than that). When atoi fails, it return 0.

What you want is convert each character of the line into an int. Something like that:

for (int i = 0; i < 30; i++)
{
    getline(m_file,line);
    for (int k = 0; k < 50; k++)
    {
        // The ASCII character of the digits 0 to 9 have
        // successives values.
        int tnum = line[k] - '0';
        grid[i][k] = tnum;
    }
}


Look at your code again. Try to see what is actually says instead of what you hope it says

int tnum = atoi(line.c_str());

You clear want that line to read each of the fifty numbers on the line in turn. But it doesn't say that. It tries to turn the whole line into an integer (and tries to do that fifty times).

Since your numbers are single digits, you actually need something much simpler

int tnum = line[k] - '0';

By saying line[k] you will get a different digit each time round the loop (because k increases each time round the loop). The - '0' bit is just a trick to turn a character into an integer. See if you can work out how it works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜