0

i have a txt file that has millions of lines, each line has 3 floats, i read it using the following code :

ifstream file(path)
float x,y,z;
while(!file.eof())
  file >> x >> y >> z;

and i works perfectly.

Now i want to try doing the same thing using Boost mapped files, so i do the following

string filename = "C:\\myfile.txt";
file_mapping mapping(filename.c_str(), read_only);
mapped_region mapped_rgn(mapping, read_only);
char* const mmaped_data = static_cast<char*>(mapped_rgn.get_address());
streamsize const mmap_size = mapped_rgn.get_size();

istringstream s;
s.rdbuf()->pubsetbuf(mmaped_data, mmap_size);
while(!s.eof())
  mystream >> x >> y >> z;

It compiles without any problem, but unfortunatly the X,Y,Z doesn't get the actual float numbers but just rubbish, and after one iteration the While is ended.

I probably doing something terribly wrong

How can i use and parse the data inside the memory mapped file ? i searched all over the internet and especially stack overflow and couldn't find any example.

I'm using windows 7 64 bit.

OopsUser
  • 4,642
  • 7
  • 46
  • 71
  • 1
    Since you're using boost already, why not make it simple and use [mapped_file_source](http://www.boost.org/doc/libs/release/libs/iostreams/doc/classes/mapped_file.html#mapped_file_source) from boost.iostreams? (also, `while(!file.eof())` is wrong in any case) – Cubbi Jul 03 '13 at 13:38
  • I'm very new to boost, how should i use it and how can i parse the floats using it ? – OopsUser Jul 03 '13 at 13:51

1 Answers1

3

Boost has a library made just for this purpose: boost.iostreams

#include <iostream>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/device/mapped_file.hpp>
namespace io = boost::iostreams;

int main()
{
    io::stream<io::mapped_file_source> str("test.txt");
    // you can read from str like from any stream, str >> x >> y >> z
    for(float x,y,z; str >> x >> y >> z; )
        std::cout << "Reading from file: " << x << " " << y << " " << z << '\n';
}
Cubbi
  • 46,567
  • 13
  • 103
  • 169
  • Thanks, strangely it no faster then just reading using the trivial ifstream. Maybe i'm missing something , or the parsing that the str does to fill the x,y,z is very slow... – OopsUser Jul 03 '13 at 14:16
  • @OopsUser It's a single pass through the file, beginning to end. There is little gain from memory mapping (it would save one memory-to-memory copy on a smart OS) – Cubbi Jul 03 '13 at 15:02