c++ - memory mapping a text file of floats separated by space to a vector of floating point numbers -
i have text file several thousands of floating point numbers separated space.i trying use memory mapping copy data text file vector of floating point numbers in visual studio 2010 using c++.following code made read text file memory.code reading random numbers makes no sense. can 1 me fixing , copying data vector of floats
#include<boost\iostreams\device\mapped_file.hpp> #include<iostream> int main() { boost::iostreams::mapped_file_source file; int numberofelements = 1000000; int numberofbytes = numberofelements*sizeof(float); file.open("replaytextfile.txt",numberofbytes); if(file.is_open()) { float* data = (float*)file.data(); for(int = 0;i <numberofelements;i++) std::cout<<data[i]<<", "; file.close(); } else { std::cout<<std::cout<<" couldnt map file"<<std::endl; } system("pause"); return 0; }
this looking @ underlying representation of input text, taking sizeof(float) bytes of , attempting treat actual float.
in typical case, float 4 bytes, given input 1.23456, it'll take 1.23, @ underlying representation (typically 0x31, 0x23, 0x32, 0x33) , interpret float.
then it'll take 456 (0x34, 0x35, 0x36, 0x20) , interpret second float.
obviously, need convert digits of 1 number 1 float, ignore space, convert digits of next number next float.
the easiest way open file stream, initialize vector<float> istream_iterators initialized file:
std::ifstream in("replaytextfile.txt"); std::vector<float> floats { std::istream_iterator<float>(in), std::istream_iterator<float>()};
Comments
Post a Comment