473,385 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 anfangsanordnung.txt.

However this is the programm:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     fstream f;
  4.     char cstring[256];
  5.     char color[6];
  6.     int x=0,y=0,var=0,var2=0;
  7.     int zahl;
  8.  
  9.     f.open("anfangsanordnung.txt", ios::in);
  10.     if (!f.eof()){f.getline(cstring, sizeof(cstring));}
  11.     while (!f.eof())
  12.     {
  13.     i=0;
  14.     x=0;
  15.     y=0;
  16.     var=0;
  17.     var2=0;
  18.     while (cstring[i]==' '){i++;}
  19.         while (cstring[i]!=' ')
  20.     {
  21.       zahl = cstring[i] - '0';
  22.       x=x*10+zahl;
  23.       i++;
  24.     }
  25.     i++;
  26.     i++;
  27.     while (cstring[i]!=' ')
  28.     {
  29.       zahl = cstring[i] - '0';
  30.       y=y*10+zahl;
  31.       i++;
  32.     }
  33.     i++;
  34.     while (var2<4)
  35.     {
  36.       while (cstring[i]!=' ')
  37.       {
  38.         color[var]=cstring[i];
  39.         var++;
  40.         i++;
  41.       }
  42.       i++;
  43.       if (color[0]=='B'){steine[x-1][y-1][var2]=0;var2++;}
  44.       if (color[0]=='G'){steine[x-1][y-1][var2]=1;var2++;}
  45.       if (color[0]=='O'){steine[x-1][y-1][var2]=2;var2++;}
  46.       if (color[0]=='Y'){steine[x-1][y-1][var2]=3;var2++;}
  47.       var=0;
  48.     }
  49.     f.getline(cstring, sizeof(cstring));
  50.     }
  51.     f.close();
  52.     cout <<  "finsihed"  << endl;    
  53. }
  54.  
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
Sep 30 '11 #1
5 4681
mac11
256 100+
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.
http://cplusplus.com/reference/string/getline/

Plus if you use std::string rather than char[] you get std::string.find() and such so a lot of those while loops can go away.
Sep 30 '11 #2
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.find() 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??
Sep 30 '11 #3
mac11
256 100+
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/
Sep 30 '11 #4
OK,
if u want steine to work then ill just send u the appropriate text

Expand|Select|Wrap|Line Numbers
  1. int steine[12][8][4];
  2.  
OK this should be all

there is the .txt file in the attachments
Attached Files
File Type: txt anfangsanordnung.txt (2.9 KB, 409 views)
Sep 30 '11 #5
Hi,

I just found my mistake in my Program my char array color[6] gets exceeded because of
Expand|Select|Wrap|Line Numbers
  1. while (cstring[i]!=' ')
  2. {
  3.   color[var]=cstring[i];
  4.   var++;
  5.   i++;
  6. }
  7.  
so thanks any way
Oct 1 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: ip4ram | last post by:
I am quite puzzled by the stack overflow which I am encountering.Here is the pseudocode //define stack structure //function operating on stack void my_stack_function( function parameters) {...
7
by: Aguilar, James | last post by:
Hello all, To begin, yes, this -is- a homework assignment. However, it is for my Algorithms class, and my instructor has given us explicit permission to use "expert" groups like newsgroups, so...
19
by: Jim | last post by:
I have spent the past few weeks designing a database for my company. The problem is I have started running into what I believe are stack overflow problems. There are two tab controls on the form...
4
by: Victor | last post by:
Hello, I've got a situation in which the number of (valid) recursive calls I make will cause stack overflow. I can use getrlimit (and setrlimit) to test (and set) my current stack size. ...
2
by: David W. Walker | last post by:
I am attempting to port a C code that runs OK on a number of Linux and Unix machines to Windows XP using Visual Studio C++. I have set the program up as a console application, but when I try to run...
5
by: jbix | last post by:
We have a stack overflow happening. We are NOT doing any recusion. We do have an object being created in Global that is being handed the Context (which I assume is specific to the request thread). ...
7
by: amit.atray | last post by:
Environement : Sun OS + gnu tools + sun studio (dbx etc) having some Old C-Code (ansi + KR Style) and code inspection shows some big size variable (auto) allocated (on stack) say for ex. char...
12
by: Geoff Cox | last post by:
Hello, Some one is getting the "stack overflow" error message when using a Javascript program which I wrote when using Internet Explorer but not when using Mozilla. Any ideas as to how I...
2
by: =?Utf-8?B?VG9tIEFsbGVu?= | last post by:
Hi, TIA for any help. (Thanks, Mattias Sjögren for your help yesterday! ) I am developing a C# wrapper for a 3rd party API. It processes buffers from a video stream (file) and is structured...
5
by: moondaddy | last post by:
I'm trying to serialize some xaml elements using the XamlWriter and I'm getting a stack overflow error because the elements have some public properties which get called about a million ( a lot)...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.