|
Boost Users : |
Subject: [Boost-users] [Interprocess] How to make a clean print using a managed file mapping?
From: Alexander Maslowski (alexmas_at_[hidden])
Date: 2010-02-05 20:27:31
Interprocess newbie question.
Im trying to use interprocess to print a data file to possibly
improve the speed of my code.
Ive attached a code sample to the bottom. The code compiles with msn
cl compiler, but segfaults with gcc (4.3.2).
Two questions:
Is it possible to produce a file that is not junked up with binary at
the beginning and end?
Any ideas why it seg-faults with gcc?
Thanks in advance.
--Alex
#include <boost/interprocess/managed_mapped_file.hpp>
#include <boost/interprocess/streams/bufferstream.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include <exception>
#include <iostream>
#include <vector>
int main( int /*argc*/, char** /*argv*/ )
{
using namespace std;
using namespace boost;
//Initialize the data to be printed
size_t dataSetSize = 10;
vector<double> coordinate(dataSetSize);
vector<double> magnitude (dataSetSize);
for( size_t i = 0; i < dataSetSize; ++i){
coordinate[i] = static_cast<double>(i);
magnitude [i] = static_cast<double>(2*i) +
static_cast<double>(2*i)/1e10;
}
vector<string> banner(2);
banner[0]="this is my data file banner\n";
banner[1]="file end";
string fileName("fileMapperTest.out");
try{
using namespace filesystem;
using namespace interprocess;
//Remove the file if it already exists
path fileNamePath(fileName);
if( exists(fileNamePath) ) remove(fileNamePath);
size_t dataWidth = 20;
size_t bufferSize =
banner[0].size()+2*dataSetSize*(dataWidth+3)+banner[1].size();
//map the file
managed_mapped_file tstFileMap( create_only, fileName.c_str(),
2*bufferSize);
cout << "file was mapped" << endl;
//create a buffer
char *aString = tstFileMap.construct<char>("aString")
[bufferSize]();
cout << "the buffer was constructed" << endl;
//stream into the buffer
bufferstream testFileBuff(aString, bufferSize, ios_base::out);
testFileBuff.precision(dataWidth-5);
cout.precision(dataWidth-5);
cout << "the buffer stream was constructed" << endl;
//Start printing
testFileBuff << banner[0] << "\n";
for( size_t i = 0; i < dataSetSize; ++i){
testFileBuff << coordinate[i] << " "
<< magnitude [i] << "\n";
cout << coordinate[i] << " " << magnitude [i] << "\n";
}
testFileBuff << banner[1] << endl;
tstFileMap.destroy_ptr(aString);
}
catch(interprocess::interprocess_exception& e)
{
cout << "An interprocess exception was thrown ..." << endl;
cout << e.what() << endl;
}
catch(...)
{
cout << "An unknown exception was thrown ..." << endl;
}
return 0;
}
My file output:
[binary junk]...
0 0
1 2.0000000002
2 4.0000000004
3 6.0000000006
4 8.0000000008
5 10.000000001
6 12.0000000012
7 14.0000000014
8 16.0000000016
9 18.0000000018
file end
[more binary junk]
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net