I have this problem. I am reading data from a file, simple text lines. The program works fine on windows, however, on unix, it reads the last line twice ... I am perplexed.
What OS and compiler are you using? I've run the thing on Windows NT 4.0 SP6a, HP-UX 10.20 with gcc 3.1, and some digital unix box with gcc 3.0 and the output is the same on each. I'm guessing it's your compiler's implementation of getline().
UX1:/data/pwendt_ux1/test/exe>gcc --version
gcc (GCC) 3.1
Copyright (C) 2002 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
--Paul
Axter
June 26th, 2002, 10:12 PM
You should use the global getline function instead.
The global getline function works with an std::string.
I think the problem in your code is due to how your loop works. The .eof() condition is not triggered until after you try to read past the end of the file. For instance, when you read the last line of the file, .eof() is not triggered, so the loop continues for one more iteration. You try to get another line from the stream, but it finds that there is no more data, leaves your buffer intact, and sets the eof bit. The only strange thing is that the buffer remains holding the same value between loop iterations. I'm guessing that your compiler found a simple optimization to make, and did not reallocate the buffer every iteration through your loop, like it is in your code. Just an efficiency thing, but it's best to put things like that, where you're allocating an object or array, outside the loop so they don't get done every time through. Anyway, I digress. Try this:
while(!(dataFile.eof() || dataFile.bad() || dataFile.fail()))
{
// If we're in here, then we know we got good data last time
cout << "buffer = " << buffer << endl;
// Add to the part list
partNameList.push_back(buffer);
// Get the next set of data, but make sure it's good before we do anything with it
dataFile.getline(buffer, HPGL_TOOL_MAX_LINE_LENGTH);
}
This should work fine, I think. If I'm totally off, someone please correct me, it's entirely possible that I am. :)
kaftab
June 27th, 2002, 08:44 AM
Thanks for your replies.
I am using windows 2000 with VC++ 6.0, and hpux 10.20 with aCC compiler.
Bob, I tried the algorithm that you provided, and it works fine on both OSs.
Thanks much!
Kamran
kaftab
June 28th, 2002, 08:58 AM
Actually, still not working! :(
The above algorithm works for unix, but now reads one line less on windows!
Axter, I tried your suggestion, works on unix, but fails on windows, here's the code
while(!(dataFile.eof() || dataFile.bad() || dataFile.fail()))
{
string buffer; // string is already using the std namespace
int main()
{
ifstream infile("input.txt");
if (!infile)
{
cout << "Problem openning file" << endl;
return 1;
}
string buffer;
while (getline(infile,buffer))
{
cout << buffer << endl;
}
return 0;
}
PaulWendt
June 28th, 2002, 09:32 AM
I've tried your original code on Visual Studio 6.0 SP5, gcc 3.0, gcc 3.0.4, and gcc 3.1. I don't think there's a problem with your original code and I'd guess your aCC compiler has "issues". Try to download gcc; it's open-source, but you'll probably have to build it yourself, as there don't seem to be too many 10.20 binaries out there. If you're going to build 3.1, I suggest you get 2.95.2 and build that first; I was unable to build 3.1 out of the box on our hp 10.20 system. Also, make sure you read the platform-specific configuration notes; you'll need gnu's binutils assembler and gnu's make and everything to install it. I'd test your code on aCC myself, but I don't have it on our 10.20 system.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.