Jump to content
  • Advertisement
Sign in to follow this  
random_thinker

C++ Bestway to read a line of data from a stream or buffer...

This topic is 4850 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi All, Thanks to Fruny and Zahlman and others the help on my skipLines question. I realize from that exercise that I might (definitely do!) need some help on getting data from an istream. I want to acquire data line-by-line, and here's what I currently use for methods in an IO specialization: for a string: string IO<ifstream>::getString() { char buffer[bs_ + 1]; this->getline(buffer,bs_); return buffer; } then overloaded getData() method for various types: void IO<ifstream>::getData(string & data) // just a wrapper for simplicity. { data = getString(); } void IO<ifstream>::getData(int & data) // integer signed, implicit cast for other ints. { data = atoi(getString().c_str()); } void IO<ifstream>::getData(float & data) // float, implicit cast for greater precision. { data = atof(getString().c_str()); } UGLY isn't it? There must be a better way. Any ideas? --random

Share this post


Link to post
Share on other sites
Advertisement
Hi Si,

This is a template specialization that adds extensions to stream objects, specialized for ifstreams.

I've added these methods to read the data line by line, and fill a struct that includes various data types, for example:

using namespace std;

Project datastruct;
intu header_size_ = 4; // unsigned int, 4 line header.
string statefile = "file.st";

Input in(statefile,header_size_); // where Input is a typedef to IO<ifstream>

in.getData(datastruct.record_no); // This is an unsigned int.
in.getData(datastruct.name); // This is a string.
in.getData(datastruct.value); // This is a float.
.
.
.
etc.

There must be another, better way to read each line of data in a cleaner and more STL-centric way...

Share this post


Link to post
Share on other sites
You seem to be making life harder on yourself than necessary. What's wrong with a function like:

template <typename T>
T read_line_as(std::istream & is) {
std::string str;
std::getline(is, str);
return boost::lexical_cast<T>(str);
}

Share this post


Link to post
Share on other sites
So, I could eliminate all those getData methods and introduce one method or a separate function that uses an input stream. I've seen the boost::lexical_cast<> referenced before, but I've resisted using it because I'm trying to stick to the ANSI C++ standard libraries, for ease of porting.

Can just this part of the boost code library be downloaded? Or are there dependencies?

--random

Share this post


Link to post
Share on other sites
I think you need to download the whole thing in order to get lexical_cast. But, you can probably just get the files you need from CVS.

You can achieve the same effect by using std::stringstream instead of boost::lexical_cast, though you'll probably want to add an extra template specialization for std::string:

template <typename T>
T read_line_as(std::istream & is) {
std::string str;
std::getline(is, str);
std::stringstream sstr(str);
T temp;
sstr >> temp;
return temp;
}

template <>
std::string read_line_as<std::string>(std::istream & is) {
std::string str;
std::getline(is, str);
return str;
}

Otherwise read_line_as<>() won't handle string types properly.

Share this post


Link to post
Share on other sites
Thank you Si, You've been a great help. What I'll do is make a lexical cast template as you've advised using stringstring, it will probably be one of the most useful templates I could have since my programme does a lot of file io.

Cheers!

--random

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!