Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using streambuf in ifstream and newline nuisance

Posted on 2002-06-25
8
Medium Priority
?
1,530 Views
Last Modified: 2008-02-01

Hi,

Env : VC++ 6 in WinNT.

I'm using ifstream and streambuf as below.

stringbuf strbufTemp;
string workBuffer;

ifstream fin(strFileName.c_str(),ios::in);

fin.get( strbufTemp);
workBuffer = strbufTemp.str();

Accroding to documented standard, if the file has a newline byte(s) "\r\n" or "\n", fin.get stops at the new line while filling the streambuf.

Is there a way to get around this (ignore newline, tab and other escape characters inserted in the file ) and fill the streambuf till end of file ?.

Thanks.
0
Comment
Question by:sindbad
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 49

Expert Comment

by:DanRollins
ID: 7109431
There are several variations of the istream.get() function.  Most of them defualt to stopping when you read a newline.  Biut in this one, for instance, it will stop when it hoits the letter ex:

 istream.get( rsb, 'x' );

I really hope this helps to solve your problem.  Really.

-- Dan
0
 
LVL 30

Accepted Solution

by:
Axter earned 200 total points
ID: 7110031
Looking at your code, a better approach would be to use the STL global getline function.
Example:
#include <iostream>
#include <fstream>
#include <string>

int main(int argc, char* argv[])
{
     std::ifstream fin("c:\\test.txt",std::ios::in);
     std::string workBuffer;

     std::getline(fin,workBuffer, fin.widen('\255'));

     std::cout << workBuffer << std::endl;
     
       return 0;
}

By using this method, you pull in the data directly to your std::string object in one shot.
Specifying 255 as your end-of-line charactor, causes the std::getline function to read the entire text file instead of reading one line.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Axter
ID: 7111581
sindbad,

You have a realy bad grading record.

Last 10 Grades Given : B C B B B B B B B B

An 'A' grade is usually awarded to most answers.
B and C grades should be reserved for answers in which you're not satisfied with what the expert has given you.
If you're not satisfied with the answer, you should let the expert know, before awarding a B or C grade.  This will give the expert a chance to give you a satisfactory answer.

Furthermore, you should either award the points for your open questions after getting an answer, or delete your old question if you did not get an answer that helped you.

Remember, experts do not get paid to help you.  The only reward they get is the points you award them.

Please show your appreciation by awarding points with an adequate grade to those experts that take the time to help you.

Thank you
0
 

Author Comment

by:sindbad
ID: 7111654

Hi Axter,

Your solution worked.

Thanks.



0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7112141
>>Specifying 255 as your end-of-line charactor, causes the std::getline function to
>>read the entire text file instead of reading one line.

I wish I had thought of that when I posted the previous day.  

-- Dan
0
 
LVL 30

Expert Comment

by:Axter
ID: 7112180
>>I wish I had thought of that when I posted the previous >>day.  

I was woundering what the 'x' was all about...:-)
0
 

Author Comment

by:sindbad
ID: 7114267

EE rating says A -> excellent, B -> good and C -> as average. If the experts' solution solves my problem, then I treat as good (B) else grade it as average (C). If the solution goes more than just answering my question like this one, I do not hesitate to give an excellent (A) rating.

Hope this explains.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

877 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