Stack Overflow with getline () ??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Noodles
    New Member
    • Sep 2011
    • 9

    Stack Overflow with getline () ??

    Hi,

    I wrote this Programm it reads out something from a file something like:

    1 2 G O B Y
    11 3 O B B Y

    from a .txt file called anfangsanordnun g.txt.

    However this is the programm:

    Code:
    int main()
    {
        fstream f;
        char cstring[256];
        char color[6];
        int x=0,y=0,var=0,var2=0;
        int zahl;
        
        f.open("anfangsanordnung.txt", ios::in);
        if (!f.eof()){f.getline(cstring, sizeof(cstring));}
        while (!f.eof())
        {
    	i=0;
    	x=0;
    	y=0;
    	var=0;
    	var2=0;
    	while (cstring[i]==' '){i++;}
            while (cstring[i]!=' ')
    	{
    	  zahl = cstring[i] - '0';
    	  x=x*10+zahl;
    	  i++;
    	}
    	i++;
    	i++;
    	while (cstring[i]!=' ')
    	{
    	  zahl = cstring[i] - '0';
    	  y=y*10+zahl;
    	  i++;
    	}
    	i++;
    	while (var2<4)
    	{
    	  while (cstring[i]!=' ')
    	  {
    	    color[var]=cstring[i];
    	    var++;
    	    i++;
    	  }
    	  i++;
    	  if (color[0]=='B'){steine[x-1][y-1][var2]=0;var2++;}
    	  if (color[0]=='G'){steine[x-1][y-1][var2]=1;var2++;}
    	  if (color[0]=='O'){steine[x-1][y-1][var2]=2;var2++;}
    	  if (color[0]=='Y'){steine[x-1][y-1][var2]=3;var2++;}
    	  var=0;
    	}
    	f.getline(cstring, sizeof(cstring));
        }
        f.close();
        cout <<  "finsihed"  << endl;    
    }
    It actually says finished and then reports about a:
    *** stack smashing detected ***: ./out terminated

    Am I getting getline() or .eof() wrong these are the only two lines new to me ??

    Thanks
  • mac11
    Contributor
    • Apr 2007
    • 256

    #2
    Not sure why it's giving you that error, but that steine array looks scarey.

    You might want to check f.good() instead of (or in addition to) f.eof() because things can go wrong other than hitting the end of the file.

    You can simplify your code greatly if you use std::string instead of char arrays. There's a std::getline that lets you read directly from a stream into a string.


    Plus if you use std::string rather than char[] you get std::string.fin d() and such so a lot of those while loops can go away.

    Comment

    • Noodles
      New Member
      • Sep 2011
      • 9

      #3
      Sorry,

      I am a mix of German and English, so my program is actually written in German
      steine for example means stones.

      Hmm about that std::string.fin d() it actually matters if it reads:
      1 2 O Y G B
      or
      1 2 Y G B O
      and i think when i use this find command it wouldn't really make things easier,
      i think.

      I am not familiar with strings, or i get confused using them,
      since this is my fifth language(Delphi ,Java,C Sharp,some Assembler stuff).

      Right now i think C++ as much higher potential.
      I am a beginner and i'll get to strings, probably soon.

      But right now there is a big contest of programming in Germany for younglings (i am 18) and I just started C++, so learning everything at once pops my learning capability, but thanks i'll write strings on my too do list.

      f.good() alone and also in addition wont solve the problem.
      But may i ask what f.good() exactly answers (is it a boolean?)
      and also what does getline() do
      and why could there be a stack overflow??

      Comment

      • mac11
        Contributor
        • Apr 2007
        • 256

        #4
        I can't compile your code because steine isn't defined in your example. I meant that steine is scarey because it's a 3 dimensional array and all that indexing might be goofed up (not because of it's name).

        I still don't know what might be clobbering your stack. You can run valgrind or gdb (assuming you're running linux) or whatever memory/debugger you have.

        f.good() checks to see if any of the streams failure bits are set (there are 3 of them), and yes it's probably not your issue at hand but it's a good habit.
        see here: http://cplusplus.com/reference/iostream/ios/good/

        Comment

        • Noodles
          New Member
          • Sep 2011
          • 9

          #5
          OK,
          if u want steine to work then ill just send u the appropriate text

          Code:
          int steine[12][8][4];
          OK this should be all

          there is the .txt file in the attachments
          Attached Files

          Comment

          • Noodles
            New Member
            • Sep 2011
            • 9

            #6
            Hi,

            I just found my mistake in my Program my char array color[6] gets exceeded because of
            Code:
            while (cstring[i]!=' ')
            {
              color[var]=cstring[i];
              var++;
              i++;
            }
            so thanks any way

            Comment

            Working...