Solved

Using streambuf in ifstream and newline nuisance

Posted on 2002-06-25
8
1,500 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 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
 
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
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.

 

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

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

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 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…

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

15 Experts available now in Live!

Get 1:1 Help Now