Problem trying to read

I have made this function, to try to read the characters in a .txt file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void Level::ReadStream()
{
    long p = textData.tellg();
    int counter = 0;
    int i = 0;
    int j = 0;
    char ch;

    while(p != textData.eof())
    {
        textData.seekg(counter);
        textData >> ch;

        if(ch != '[' && ch != ']')
        {
            mapX[i] = ch; // mapX is a vector, 5 int long.
            mapY[j] = ch; // same as mapX.

            i++;

            if(ch == '\n')
            {
                i = 0;
                j++;
            }
        }

        counter++;
        p = textData.tellg();
    }
};

My program compiles, but hangs on and must be aborted. I really don't know what is wrong with it. Can anyone help?
Last edited on
well try this

1
2
3
4
5
6
7
8
9
10
void level::readstream()
{
     string input;
     ofstream file;

     while(cin>> input)
     {
          file<< input;
     }
}


use that as the base
I don't see it helps me in this case. Could someone explain what exactly is wrong?
I think the extraction operator ignores whitespace by default so this line will never return '\n'

textData >> ch;

Last edited on
textData.eof() return a true if the it has reached EOF (end of file), otherwise false. It does not make sense to compare that value to the value you got from tellg().

I don't really understand why you are using seekg in your code but generally the correct way to read from a stream is what Aramil of Elixia showed you. I think this page has a good explanation: http://www.parashift.com/c++-faq-lite/input-output.html
vin wrote:
I think the extraction operator ignores whitespace by default so this line will never return '\n'

If you want to read one char at a time and don't skip whitespaces you can use the get member function.
Topic archived. No new replies allowed.