Link to home
Start Free TrialLog in
Avatar of prashant_n_mhatre
prashant_n_mhatre

asked on

STL getline fails

I'm using 'getline' for reading data from a file and writing it to another file.
like
  getline(inputFileObject,string,'\n')
It fails when there is occurance of the following character pattern
(\r\n\r\n\r\n)

0D 0A 0D 0A 0D 0A

characters. My OS is DYNIX/PTX (UNIX)

The same code works in VC++ 6.0.

Any idea?

(using 'strings' command and processing the input file works. but it looses format)


Avatar of DanRollins
DanRollins
Flag of United States of America image

In what way does it fail?  I would expect it to return three empty strings.  What is it doing?

-- Dan
Avatar of prashant_n_mhatre
prashant_n_mhatre

ASKER

Here is the dump of the portion:

http://www.onesmartclick.com/books/dump.gif

It writes till '<html>' then doesn't write anything to file.

This is the test code I'm using:


void writeToFile(ifstream& inpFile)
{
    string refLine;
    ofstream fileout ("output.txt");
    while ( getline(inpFile,refLine,'\n') )
    {
        fileout << refLine;
        fileout << "\n";
    }
    fileout.close();
}

May be the code is buggy.

You should definately get rid of those constants, i.e. "\n" is bad -- the stl does have a replacement for that, that works across platforms: std::endl -- use this and see if it solves your problem.
It has something to do with the idiotic STL and namespace.  This sequence works because I waved a magic std wand over it:

#include <fstream>
#include <string>

void writeToFile( std::ifstream& inpFile )
{
     std::string refLine;
     std::ofstream fileout ("c:\\temp\\output.txt");
     while ( std::getline( inpFile, refLine, '\n' ) ) {
          fileout << refLine;
          fileout << "\n";
     }
     fileout.close();
}

void main()
{
     std::ifstream inpFile( "c:\\temp\\input.txt" );
     writeToFile(inpFile);
}

-- Dan
hi prashant_n_mhatre,
Do you have any additional questions?  Do any comments need clarification?
-- Dan
I tried out both options..nothing works... Works on Windows but doesn't work on DYNIX/PTX... Is it possible that stl library on DYNIX/PTX is buggy?
>> Is it possible that stl library on DYNIX/PTX is buggy?

It is possible.  But I ask that you try the sequence I provided, without change of any kind... it is a complete program.  Verify that it fails *only* when the input file has exactly four sequential sets of 0x0d 0x0a bytes.  Does it also fail with three or two in a row?

You might also want to try opening the file in binary mode (that was my first guess, but I stopped short of that when I got my sample working).

-- Dan
It doesn't compile on my machine. It says

"dan.cpp", line 8: error #304: no instance of overloaded function
          "std::getline" matches the argument list
            argument types are: (std::ifstream, std::string, char)
                        while ( std::getline( inpFile, refLine, '\n' ) ) {
                                ^

1 error detected in the compilation of "dan.cpp".
c++: Fatal Error: Compilation failed
ASKER CERTIFIED SOLUTION
Avatar of DanRollins
DanRollins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yep..I also think so..Thanks for your help !!!