Hi guys.Does anyone who studied the implementation of stringstreams know if :

char c;
   std::stringstream ss;

   ss << std::fstream ("file.xml",std::ios::in).rdbuf();//this is eqaul in terms of speed
  
   while (ss.get(c))  {} //with this?

   std::ifstream f("file.xml");
   while (f.get(c)) {} //or this?

Thanks a lot.

I don't understand the question. Are you asking if reading from a stringstream is faster than reading from an fstream?

I think i have several questions and a problem in how to put them.
1) Is the extraction operator using the get() function in case of fstream?
2) Does moving the file pointer imply another call to seekg()?
3) Would this mean stringstream's get() function would be faster?
4) Would this 'ss << std::fstream ("file.xml",std::ios::in).rdbuf();' be faster than both operations since the buffer is already filled?

I hope i'm not making a fool of myself :)

1) Is the extraction operator using the get() function in case of fstream?

Probably not. More likely is that it uses the member functions of the stream buffer directly (just like get()).

2) Does moving the file pointer imply another call to seekg()?

No, the file pointer is at a much lower level than seekg().

3) Would this mean stringstream's get() function would be faster?

It's fairly safe to assume that stringstream's get() function is faster, but that's because you're accessing strings in memory rather than hardware.

4) Would this 'ss << std::fstream ("file.xml",std::ios::in).rdbuf();' be faster than both operations since the buffer is already filled?

Well, obviously it comes down to the quality of the implementation, but writing the stream buffer is typically the fastest way to dump the contents of a stream.

Reading from a hard drive is typically around a million times slower than reading/writing to memory. I read that somewhere, don't quote me.

It would only make sense.Anyway after a bit more study i think the fastest possible way is by using the streambuf class methods directly or the i/ostringstream_iterators. I also could use someone's approval regarding that statement.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.