getline related error?

getline related error?

Page 1 of 1

11 Replies - 1823 Views - Last Post: 27 July 2010 - 11:09 PM Rate Topic: -----

#1 Wereworm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-June 07

getline related error?

Posted 27 July 2010 - 10:29 AM

i was making a program and everything went suspiciously smooth when suddenly i encountered an error

there was no problem compiling, but the program crashed instantly

i narrowed it down to the getline function

it had been working flawlessly before and the code i added should not have interfered with it


i stripped the code down as much as i could until all that was left was this below

it's not meant to do anything ofcourse, just to show what's wrong

i'm sorry if it's just a stupid mistake but i can't see anything wrong



#include <iostream>
#include <fstream>



using namespace std;



string read;


string search()
{
  
  ifstream myfile ("myfile.txt");
  if (myfile.is_open())
 {
    while (! myfile.eof() )
  {
  
 
  
  getline (myfile,read);
  
  }
 }




}



int main()
{
  

   while(1==1){


 search();

 


    }
    
    
}




Is This A Good Question/Topic? 0
  • +

Replies To: getline related error?

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: getline related error?

Posted 27 July 2010 - 11:20 AM

So....this program crashes?

Also, eof() doesn't do what you think it does. Do tell me what you think it does, and tell us how you know what it does.
Was This Post Helpful? 0
  • +
  • -

#3 Wereworm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-June 07

Re: getline related error?

Posted 27 July 2010 - 05:07 PM

View PostOler1s, on 27 July 2010 - 10:20 AM, said:

So....this program crashes?

Also, eof() doesn't do what you think it does. Do tell me what you think it does, and tell us how you know what it does.



yes... this program crashes

i thought it would be some kind of compiler bug since even a very small piece of code like this behaves strangely

but i have overlooked tiny details before so i could not be certain

what is your opinion?


also eof() i imagine checks if the stream has reached the end of file symbol in a file, in this particular case it continues to read until the file ends i think

i don't see how this is relevant however, since it's not intended as a real program, only as a test to see what is wrong
Was This Post Helpful? 0
  • +
  • -

#4 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: getline related error?

Posted 27 July 2010 - 05:33 PM

I have modified your program a bit. Why? Because your program doesn't even compile for me. Here's the modified code:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

string read;

string search()
{
    ifstream myfile ("myfile.txt");
    if (myfile.is_open())
    {
        while (! myfile.eof() )
        {
            getline (myfile,read);
        }
    }
    return read;
}

int main()
{
    while(true) { search(); }
}



This code runs without problems (there's an infinite loop, but the program doesn't crash). You'll notice that I added in an include (string), and I also returned something from the function. Now, if the above code crashes for you, you can start looking at what your compiler is doing.

I'm surprised that you were able to compile your original code. You wrote a function that didn't return anything, yet was declared to return a std::string. Hmmm.

Quote

also eof() i imagine checks if the stream has reached the end of file symbol in a file, in this particular case it continues to read until the file ends i think
Instead of imagines and thinks, you should be certain about the behavior. Consider reading documentation, instead of guessing at behavior...

This post has been edited by Oler1s: 27 July 2010 - 05:34 PM

Was This Post Helpful? 0
  • +
  • -

#5 Wereworm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-June 07

Re: getline related error?

Posted 27 July 2010 - 06:21 PM

View PostOler1s, on 27 July 2010 - 04:33 PM, said:

I have modified your program a bit. Why? Because your program doesn't even compile for me. Here's the modified code:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

string read;

string search()
{
    ifstream myfile ("myfile.txt");
    if (myfile.is_open())
    {
        while (! myfile.eof() )
        {
            getline (myfile,read);
        }
    }
    return read;
}

int main()
{
    while(true) { search(); }
}



This code runs without problems (there's an infinite loop, but the program doesn't crash). You'll notice that I added in an include (string), and I also returned something from the function. Now, if the above code crashes for you, you can start looking at what your compiler is doing.

I'm surprised that you were able to compile your original code. You wrote a function that didn't return anything, yet was declared to return a std::string. Hmmm.

Quote

also eof() i imagine checks if the stream has reached the end of file symbol in a file, in this particular case it continues to read until the file ends i think
Instead of imagines and thinks, you should be certain about the behavior. Consider reading documentation, instead of guessing at behavior...




i thank you for your patience

although when i compile the modified code it still crashes

i need to know why the getline function seems to be malfunctioning
my conclusion is that it is probably a compiler related bug

it is critical that i know this so i can remedy it
otherwise i have to drop the entire project

any suggestions?
Was This Post Helpful? 0
  • +
  • -

#6 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: getline related error?

Posted 27 July 2010 - 06:25 PM

It's possible that it is a compiler bug, but you would want to eliminate all other possible causes.

1. What compiler are you using?

2. What happens if you replace getline (myfile,read); with getline(cin, read); ? Does the program still crash?

3. What happens if you remove the while loop? Does the program still crash?
Was This Post Helpful? 0
  • +
  • -

#7 Wereworm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-June 07

Re: getline related error?

Posted 27 July 2010 - 07:51 PM

i am using dev-c++, i have heard that there are some bugs which never got fixed, so i suppose this could be one.

removing any loop alters nothing, but replacing getline(myfile,read); with getline(cin, read); stops the program from crashing, but it's no substitute for getline(myfile,read); is it?

file manipulation is critical to the real program
Was This Post Helpful? 0
  • +
  • -

#8 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: getline related error?

Posted 27 July 2010 - 08:10 PM

Quote

i am using dev-c++, i have heard that there are some bugs which never got fixed, so i suppose this could be one.
The old MinGW distribution does have bugs. It's pretty old actually. The version you have is 3.4, while gcc and corresponding MinGW distributions are past 4.5...

Definitely ditch this compiler. Two choices:

1) VC++.
2) Update MinGW. Search TDM-GCC.
Was This Post Helpful? 1
  • +
  • -

#9 Wereworm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-June 07

Re: getline related error?

Posted 27 July 2010 - 08:29 PM

thank you for your assistance


unfortunately i have installed VC++ in an attempt to compile the program, but i got so many errors that i did not even begin to comprehend that i could not continue, other compilers have been to complicated to even install

but as long as i know that it's a compiler bug i can search for a better one

thank you
Was This Post Helpful? 0
  • +
  • -

#10 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: getline related error?

Posted 27 July 2010 - 08:35 PM

Quote

unfortunately i have installed VC++ in an attempt to compile the program, but i got so many errors that i did not even begin to comprehend that i could not continue,
You should fix those errors. A modern compiler like VC++ is catching fatal mistakes.

You can, also, try setting up TDM-GCC (an up to date MinGW distribution). I just checked your code with a recent version, and it behaves correctly. But I suspect you'll get the same as VC++: a number of compiler errors from mistakes in your program.

EDIT: Make sure you turn on all warnings! There's a setting called -Wall, and it's either directly noted as Wall or "all compiler warnings" or something like that in an IDE.

EDIT 2: No, my mistake, the latest version does crash as well.

This post has been edited by Oler1s: 27 July 2010 - 08:39 PM

Was This Post Helpful? 0
  • +
  • -

#11 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: getline related error?

Posted 27 July 2010 - 08:53 PM

Here's an idea, change read to line. For example: string read;
to string line;

Do you still get a crash?

This post has been edited by Oler1s: 27 July 2010 - 08:53 PM

Was This Post Helpful? 1
  • +
  • -

#12 Wereworm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-June 07

Re: getline related error?

Posted 27 July 2010 - 11:09 PM

uuhh... it seems to work, i have not checked if the real program functions as it should now but it least it doesn't crash instantly.

so i remain cautiously optimistic.

i don't understand how i can work, did the name "read" conflict with some standard function?


anyway, thank you again. knowing my luck more crashes will probably follow, but for now it seems to work.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1