Solved

Using streambuf in ifstream and newline nuisance

Posted on 2002-06-25
8
1,512 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 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
Technology Partners: 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

728 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