getline() reading whole .txt file

Greetings,

I am currently making a small console game to get to grips with the basics of the basics in C++ and have encountered the following issue : I'm creating an ifstream in order to read a .txt file, the first two lines correspond to integers, and the third is for a grid of blocks (using a 1D array) of dimensions those two integers.

However, the getline call seems to read the whole file at the first pass. Looking this issue up, it seems to be some trouble with encoding endline statements in text editors. Does anyone know more about this ? I use Windows 7 basic text editors such as Notepad and Notepad++. Also, from what I've seen, this issue is pretty uncommon ; I'd have expected it to be a lot more common as it's pretty much the first time I've tried reading from a file.

P.S : I'm not new to programming, just new to C++.
P.P.S : Also, if you see any bad C++ programming practices in there, please point them out.

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
Labyrinth::Labyrinth(string file){
    ifstream labread;
    labread.open(file.c_str());

    if(labread.is_open()){
        string labs = "";
        int i = 0;
        while(getline(labread,labs)){
            if(i == 0){
               cout << labs<<endl; // Whole text file is displayed
               lines = atoi(labs.c_str());
            }else if(i==1){
            columns = atoi(labs.c_str());
            cout << columns<<endl; // Nothing, code never reached
            }else if(i==2){
            //some unimportant stuff
            }
        }

    lab = new Block[lines * columns];
    labs.erase(1,0);
    labs.erase(1,labs.size()-1);
    for(unsigned int i = 0; i < labs.size()-1; i++){
   *(lab+i) = convertChar(labs[i]);
    }
    labread.close();
}
}
Last edited on
The most likely culprit is that your text file is using Unix line endings, but Window line endings are expected.

If you visit Notepad++ and look under the edit menu, you'll find an option titled "EOL Conversion." You may want to take advantage.
Thanks for your reply.

Nope, it already is under Windows format (CR+LF). At least, I'm assuming it is as the option is grayed out and unclickable when the others aren't.
Please show how the input file was written, and if possible post a small sample of your input file.


All right. I used Notepad++, here's the file :

1
2
3
11"
19"
"    MMMMM              M   M              MC  M            MMM  CMM           M  C C M         MMM M MM M   MMMMMMM   M MM MMMMM  GGMM C  C          GGMMMMMM MMM MHMM  GGM   M     MMMMMMMMM    MMMMMMM         "


At first I didn't have the " symbols but I reckoned an easy way to circumvent the issue I have is to use those as delimiters. However, it'd be silly to do that and not understand what my problem is.
Last edited on
Looking this issue up, it seems to be some trouble with encoding endline statements in text editors.

What do you mean about "encoding endline statements"? Normally "endline statements" in a text editor are signified by the "Enter" key.

Last edited on
In your while loop you don't increment the variable "i".
In your while loop you don't increment the variable "i".


That could be a problem. Usually variables named 'i' are simple counts/indexes, but yours appears to represent state, and your state doesn't change. (In which case line 10 does not output the entire file, but multiple iterations of the loop invoke line 10 repeatedly until input fails.) This would be easily observable with a debugger.
All right, problem solved. Sorry, that was a dumb mistake ><
Yeah, I need to get to grips with the debugger.

Thanks very much, everyone.
Topic archived. No new replies allowed.