Solved

Using streambuf in ifstream and newline nuisance

Posted on 2002-06-25
8
1,497 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

762 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

19 Experts available now in Live!

Get 1:1 Help Now