Solved

STL getline fails

Posted on 2002-06-28
10
1,365 Views
Last Modified: 2008-02-26
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)


0
Comment
Question by:prashant_n_mhatre
  • 5
  • 4
10 Comments
 
LVL 49

Expert Comment

by:DanRollins
ID: 7117365
In what way does it fail?  I would expect it to return three empty strings.  What is it doing?

-- Dan
0
 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 7117489
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.

0
 
LVL 8

Expert Comment

by:fl0yd
ID: 7123793
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.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7123920
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
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7128447
hi prashant_n_mhatre,
Do you have any additional questions?  Do any comments need clarification?
-- Dan
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 7128641
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?
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7128744
>> 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
0
 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 7132069
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
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 50 total points
ID: 7132574
The compiler must be buggy.  The first param is a std::ifstream, the second param is a std::string, and the third parm is a char.  So the error message is bogus.  Stick to VC++ Its the only compiler worth using.

-- Dan
0
 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 7165544
Yep..I also think so..Thanks for your help !!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Handling string inputs in C/Linux 23 179
Making a Sample Win32 DLL project Using Visual Studio 2010 Professional 4 91
C++ assignment question 7 169
Best book to learn C++ 4 70
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now