Solved

Using streambuf in ifstream and newline nuisance

Posted on 2002-06-25
8
1,506 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 32

Expert Comment

by:jhance
ID: 7109451
0
 
LVL 30

Accepted Solution

by:
Axter earned 50 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
Industry Leaders: 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

732 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