0

Those are the parts of the code I have:

ifstream inFile;
inFile.open("Product1.wrl");
...
if (!inFile.is_open()){
    cout << "Could not open file to read" << endl;
    return 0;
}
else 
    while(!inFile.eof()){
        getline(inFile, line);
        cout << line << endl;  //this statement only to chech the info stored in "line" string
        if (line.find("PointSet"))
            inFile >> Point1;
    }

The output shows me the same string over and over again. So this means that the cursor inside the file does not proceed and getline reads the same line.

What might be the problem of this odd behavior?

If this is relevant: The file does open as a .txt file and contains the exact information I need.

Okay I figured the problem: Even after first eteration the return value of line.find("PointSet")is: 429467295... while my line string contains only one letter "S". Why?

Alexandru Barbarosie
  • 2,772
  • 3
  • 22
  • 42
  • I don't suppose we can be so fortunate as to examine at least one *full* data point set from that file? The code as-written reads a full line and other than checking for the word "PointSet" somewhere within, promptly ignores everything else and throws it away, then relies on an extraction operator for reading an untyped mystery object we can only hope is implemented correctly. And the use of `.eof()` is all-but-guaranteed to be wrong. – WhozCraig May 17 '13 at 14:05
  • Okay, let's start from the end. Why is `.eof()`wrong? It doesn't matter what `Point` reperesnts and how the operator is overloaded as the program doesn't get to that line as it is stuck in an infinite loop. Yes, it ignores it as I do not need it, that's one of the purposes. The file itself is >200 lines. – Alexandru Barbarosie May 17 '13 at 14:52
  • See [this question and answer](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) for why `.eof()` is almost *always* wrong as a loop condition checkpoint. Without the remaining requested info from you, thats all I can pony up. – WhozCraig May 17 '13 at 14:58

1 Answers1

0

Change

while(!inFile.eof()){
    getline(inFile, line);

to

while( getline(inFile, line) ) {

I don't know why people get bitten by eof() quite so often, but they do.

Mixing getline with >> is problematic, because the >> will leave a '\n' in the stream, so the next getline will come back empty. Change that to use getline as well.

if (line.find("PointSet")) isn't what you want either. find returns the position in the string, or std::string::npos if it wasn't found.

Also, you can change

ifstream inFile;
inFile.open("Product1.wrl");

to

ifstream inFile ("Product1.wrl");

Here's a version showing the reads:

class Point 
{
public:
    int i, j;
};

template <typename CharT>
std::basic_istream<CharT>& operator>>
    (std::basic_istream<CharT>& is, Point& p)
{
    is >> p.i >> p.j;
    return is;
}

int main()
{
    Point point1;
    std::string line;
    while(std::getline(std::cin, line))
    {
        std::cout << line << '\n';  //this statement only to chech the info stored in "line" string
        if (line.find("PointSet") != std::string::npos)
        {
            std::string pointString;
            if (std::getline(std::cin, pointString))
            {
                std::istringstream iss(pointString);
                iss >> point1;
                std::cout << "Got point " << point1.i << ", " << point1.j << '\n';
            }
            else
            {
                std::cout << "Uhoh, forget to provide a line with a PointSet!\n";
            }
        }
    }

}
BoBTFish
  • 17,936
  • 3
  • 49
  • 73
  • If I make it `while( getline(inFile, line) ) ` it reads only the first line and stops. – Alexandru Barbarosie May 17 '13 at 12:43
  • Well yes, the code is working, but still it doesn't solve the problem. I assume that the problem might be in the folder itself, but I can't imagine what might make it behave like that. – Alexandru Barbarosie May 17 '13 at 14:56
  • @AlexandruBarbarosie What is the problem you are having with my code? You still see the same string over and over? – BoBTFish May 17 '13 at 15:03
  • Okay, I figured it out, the problem is in `line.find("PointSet")`it somehow returns an int value != 0 which might be even some trash. And also it seems my overloaded operator is working a little bit wrong too (but not sure) Anyway thank you for the help. – Alexandru Barbarosie May 17 '13 at 15:39
  • @AlexandruBarbarosie If you read my post I already explained that this is an issue. `std::string::find` returns the position of the found string within the original string, or the special value `std::string::npos` if it was not found. – BoBTFish May 17 '13 at 16:06
  • Incidentally, I just posted [Reading Input with `std::getline`](http://chris-sharpe.blogspot.co.uk/2013/05/reading-input-with-stdgetline.html). – BoBTFish May 28 '13 at 21:30