• 12
• 11
• 9
• 10
• 13

Problem with getline

This topic is 3972 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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");


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");


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

Share on other sites
Try using the std::getline in the string header

std::string line;std::getline(your_stream,line);

Share on other sites
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 on other sites
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 on other sites
Quote:
 Original post by CIJollyMy 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 CIJollyBy 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 In this more advanced program I am having trouble with at the moment, I am using #include If I try to use#include 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 symbolc:\programming\game engine 19 - giant\copy (9) of skeleton\game.cpp(2216) : error C2872: 'ifstream' : ambiguous symbolc:\programming\game engine 19 - giant\copy (9) of skeleton\game.cpp(2822) : error C2872: 'ofstream' : ambiguous symbolc:\programming\game engine 19 - giant\copy (9) of skeleton\game.cpp(2859) : error C2872: 'ifstream' : ambiguous symbolc:\programming\game engine 19 - giant\copy (9) of skeleton\game.cpp(2926) : error C2872: 'ofstream' : ambiguous symbolSo, 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::vectorint 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:

I meant to use reserve(). :)

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

Share on other sites
Quote:
 std::vector strings(STRING_COUNT);

Share on other sites
Quote:
 Original post by CIJollyBy program jams, I meant I got an illegal operation error.

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 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 on other sites
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 on other sites
Quote:
 Original post by CIJollyThanks 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 on other sites
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.