Sign in to follow this  
CIJolly

Problem with getline

Recommended Posts

CIJolly    282
I'm using fstream to handle the file in/out for my program. My conversation editor saves files to a txt file, and my game loads this txt file. My problem is how to load strings with spaces from the txt file. When I use this code:
ifstream fin ("conversation1.txt");
char* loadsentencetext = new char[50];
fin.getline(loadsentencetext, 50);

the program jams. But, when I go through the .txt file and remove all the spaces within a sentence, and change the code to this:
ifstream fin ("conversation1.txt");
char* loadsentencetext = new char[50];
fin >> loadsentencetext;

everything works fine. Anyone have any ideas what I'm doing wrong with getline?

Share this post


Link to post
Share on other sites
MaulingMonkey    1730
... Works Here™.

Elaborate on: "the program jams". This could mean any number of things.

I would also clothe your naked character buffers with a std::string, using std::getline( fin , string ); to load any number of characters rather than being limited to whatever you guessed ahead of time to be sufficient.

Share this post


Link to post
Share on other sites
CIJolly    282
By program jams, I meant I got an illegal operation error.

I wrote another variation, that looks like this:


ifstream fin ("conversation1.txt");
char* getlinestring = new char[50];
char* finstring = new char [50];

fin.getline(getlinestring, 50);
fin >> finstring;




And this also works fine. However, if I remove the line fin>>finstring, the program has an illegal operation error. If I remove fin.getline, the program continues to work.
If I output finstring, I get the word from the text file. If I output getlinestring, I get nothing.

So, the reason it was jamming was because getline wasn't doing anything at all, and wasn't advancing the place the program was reading the text file from to the next line (the next few lines contain ints, so the program would hit getline, do nothing, then start trying to read ints from a string).


I made a console app where getline works fine. However, in that program, I used the include
#include <fstream.h>

In this more advanced program I am having trouble with at the moment, I am using
#include <fstream>
If I try to use
#include <fstream.h>
with the more advanced program I am currently working on, I get these errors:

:\programming\game engine 19 - giant\copy (9) of skeleton\game.cpp(2149) : error C2872: 'ifstream' : ambiguous symbol
c:\programming\game engine 19 - giant\copy (9) of skeleton\game.cpp(2216) : error C2872: 'ifstream' : ambiguous symbol
c:\programming\game engine 19 - giant\copy (9) of skeleton\game.cpp(2822) : error C2872: 'ofstream' : ambiguous symbol
c:\programming\game engine 19 - giant\copy (9) of skeleton\game.cpp(2859) : error C2872: 'ifstream' : ambiguous symbol
c:\programming\game engine 19 - giant\copy (9) of skeleton\game.cpp(2926) : error C2872: 'ofstream' : ambiguous symbol

So, for some reason getline does nothing, but it does in my more simple console program, but I have to use a different header in the simpler program.
If this doesn't work, I might just change all my spaces into asterixes or something, output them to the file, input them with fin, then change all the asterixes back to spaces...

Share this post


Link to post
Share on other sites
tasen    148
Quote:
Original post by CIJolly
My problem is how to load strings with spaces from the txt file.


How are you trying to read the strings in? Are you trying to read one string at a time or read in a whole line and break the line up into strings (using the space character as a delimiter)? I would recommend the latter (especially if you have a large file).

Quote:
Original post by CIJolly
By program jams, I meant I got an illegal operation error.

I wrote another variation, that looks like this:

*** Source Snippet Removed ***

And this also works fine. However, if I remove the line fin>>finstring, the program has an illegal operation error. If I remove fin.getline, the program continues to work.
If I output finstring, I get the word from the text file. If I output getlinestring, I get nothing.

So, the reason it was jamming was because getline wasn't doing anything at all, and wasn't advancing the place the program was reading the text file from to the next line (the next few lines contain ints, so the program would hit getline, do nothing, then start trying to read ints from a string).


I made a console app where getline works fine. However, in that program, I used the include
#include <fstream.h>

In this more advanced program I am having trouble with at the moment, I am using
#include <fstream>
If I try to use
#include <fstream.h>
with the more advanced program I am currently working on, I get these errors:

:\programming\game engine 19 - giant\copy (9) of skeleton\game.cpp(2149) : error C2872: 'ifstream' : ambiguous symbol
c:\programming\game engine 19 - giant\copy (9) of skeleton\game.cpp(2216) : error C2872: 'ifstream' : ambiguous symbol
c:\programming\game engine 19 - giant\copy (9) of skeleton\game.cpp(2822) : error C2872: 'ofstream' : ambiguous symbol
c:\programming\game engine 19 - giant\copy (9) of skeleton\game.cpp(2859) : error C2872: 'ifstream' : ambiguous symbol
c:\programming\game engine 19 - giant\copy (9) of skeleton\game.cpp(2926) : error C2872: 'ofstream' : ambiguous symbol

So, for some reason getline does nothing, but it does in my more simple console program, but I have to use a different header in the simpler program.
If this doesn't work, I might just change all my spaces into asterixes or something, output them to the file, input them with fin, then change all the asterixes back to spaces...


1. Prefer to use std::string instead of char*.
2. Header files in the CPP standard library do not end in ".h".
3. As MaulingMonkey and XXX_Andrew_XXX said, use std::getline, not std::istream::getline.
4. Check if your input stream is_open().
5. You shouldn't have to worry about changing the file format. Adapt the code to suit the file format.

Here's an example of how I'd read in the strings (I'm assuming you need to keep the string values somewhere in the program to process them):


#include <algorithm> // for std::copy
#include <iostream> // for std::cout
#include <iterator> // for std::ostream_iterator
#include <fstream> // for std::ifstream
#include <sstream> // for std::stringstream
#include <string> // for std::string
#include <vector> // for std::vector

int main()
{
// Store all the strings from the file into a vector.
std::vector<std::string> strings;

// Initially allow space for 50 strings.
const size_t STRING_COUNT = 50;
strings.reserve(STRING_COUNT);

std::ifstream fin("conversation1.txt");

// Make sure the file is actually open.
if (fin.is_open())
{
std::string line;

// Keep reading lines until we reach the end of the file.
while (getline(fin, line))
{
// Tokenize the current line into strings.
std::string temp;
std::stringstream ss(line);

// Extract the strings
while (ss >> temp)
{
strings.push_back(temp);
}

// Clear the stringstream for more input.
ss.str("");
}
}

// Print out the strings we have.
std::copy(strings.begin(), strings.end(), std::ostream_iterator<std::string>(std::cout, " "));

// strings[0] = the first string
// strings[1] = the second string
// etc.
}




Edit:

Quote:
Original post by ToohrVykWhy does your vector of strings start with STRING_COUNT empty strings?


I meant to use reserve(). :)

[Edited by - tasen on May 5, 2007 5:05:31 AM]

Share this post


Link to post
Share on other sites
ToohrVyk    1596
Quote:
std::vector<std::string> strings(STRING_COUNT);


Why does your vector of strings start with STRING_COUNT empty strings?

Share this post


Link to post
Share on other sites
MaulingMonkey    1730
Quote:
Original post by CIJolly
By program jams, I meant I got an illegal operation error.


Glare There are many illegal operation errors. Now that I know you're at least talking about a crash of some sort, I can ask for the exact wording copied and pasted wording of that error message.

Quote:
I wrote another variation,

Ahh, the "change random shit and see what happens" method of debugging. I hate it already.

For removing fin>>finstring to have an effect, chances are your error is occuring after all this, not where you've indicated. How are you determining the location of the crash -- a debugger? At this point I'm going to ask for full program source (and sample text file while you're at it), because, well, Works Here™.

Quote:
So, the reason it was jamming was because getline wasn't doing anything at all,

Slow down there cowboy, you're equating percieved symptoms to cause. Plus, you've indicated that getline clearly was doing something. That is, crashing the program. Which typically means "specifically that call", but I'm no longer certain we've been on the same page on this.

Quote:

If I try to use
#include <fstream.h>
with the more advanced program I am currently working on, I get these errors:


That would be because <fstream.h> is a prestandard header (to a language that's been standardized for 9 years now), confusing your compiler with multiple definitions of those classes. Use only the C++ headers without the .h, as only these are standard. Mixing the two will bring, well, those errors.

Quote:
If this doesn't work, I might just change all my spaces into asterixes or something, output them to the file, input them with fin, then change all the asterixes back to spaces...


Changing things at random to try and fix problems is just about the worst thing you can do in programming, as it will only make things worse in general. Figure out the problem, figure out the root cause (we're not there yet, only a string of disjoint symptoms so far...), and fix it.

Share this post


Link to post
Share on other sites
CIJolly    282
Thanks for all the help, I made the changes you suggested, but I'm still having the same problem.

I have rewritten the program to replace white space in the form of spaces with asterixes. Not a pretty fix, but it works.

Thanks again.

Share this post


Link to post
Share on other sites
MaulingMonkey    1730
Quote:
Original post by CIJolly
Thanks for all the help, I made the changes you suggested, but I'm still having the same problem.


... Given that you've ignored these two bits of my post, I can't really help you further, sorry:

Quote:
Original post by MaulingMonkey
At this point I'm going to ask for full program source (and sample text file while you're at it), because, well, Works Here™.

Quote:
If this doesn't work, I might just change all my spaces into asterixes or something, output them to the file, input them with fin, then change all the asterixes back to spaces...


Changing things at random to try and fix problems is just about the worst thing you can do in programming, as it will only make things worse in general. Figure out the problem, figure out the root cause (we're not there yet, only a string of disjoint symptoms so far...), and fix it.

Share this post


Link to post
Share on other sites
CIJolly    282
Sorry, didn't mean to ignore you, quite the opposite really.

At the moment the program is thousands of lines of code. The problem could be occuring anywhere. I wouldn't want to subject you to it.

I've been trying to track it down for 5 hours on my one free weekend this month, to no avail. I kinda gave up and took the option that gave me some progress. Sorry for wasting your time.

Share this post


Link to post
Share on other sites
ToohrVyk    1596
Full code is not necessary yet. But we would need:
  • The exact line in your code which causes the crash.
  • The exact description of the crash (text of the message).
  • The declaration of any variables that are part of that line.
  • The value/state of these variables when the program crashes.
  • The contents of the file (and the position of the read cursor) when the program crashes.


All of these should be fairly trivial to get with your debugger.

Share this post


Link to post
Share on other sites
MaulingMonkey    1730
Hmm Honestly, 1K LOC is pretty trivial. Obviously, if you can narrow it down to the actual problem area, that'd be preferable, but you don't seem to have been able to accomplish that, yet. So instead of playing source code basketball -- getting dribbles of code piece by piece -- I'd rather just blaze through it.

Normally just the nearby code sufficies, but it sounds like you've got some sort of memory related error -- and those are the worst, given that the source of the problem can occur literally anywhere. In large projects, these are typically combated with specialized memory debugging tools (for detecting leaks and overruns), as well as unit testing -- which can help uncover problems sooner rather than later -- allowing developers to focus their debugging efforts on whatever changed last.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this