-4

I have a vector of floating point numbers , i am trying to find out the total number of segments in the vector each containing 72 floating point numbers.I am filling the vector with the floating point numbers read from a text file.I am trying to find it from size of both the text file and the vector, would they be the same ?.And would they give the right answer.Following is the way i used This way gives me a value that is greater than the actual value , can some one help ?

long fileSizeVector;
long fileSize;
long fileSizes;
vector<float> ReplayBufferVector;
ifstream in;
in.open("fileName.txt");
if(in.is_open())
{
    in.setf(ios::fixed);
    in.precision(3);
    in.seekg(0,ios::end);
   fileSizes = in.tellg();
   fileSize = fileSizes/((72)*sizeof(float));
   in.seekg(0,ios::beg);

   while(!in.eof())
   {
   for(float f;in>>f;)
       ReplayBufferVector.push_back(f);
   }
   fileSizeVector = ReplayBufferVector.size()/72*sizeof(float);
in.close();
}
sarath
  • 69
  • 2
  • 7
  • 1
    This `while` is useless. What is it even supposed to do? – Baum mit Augen Jun 15 '14 at 21:52
  • While loop is for filling the vector with the contents of the text file. – sarath Jun 15 '14 at 21:54
  • `while(!in.eof())` also check http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong The `for()` loop seems to be sufficient. And remove all this also useless `fileSizes = in.tellg();` stuff ... – πάντα ῥεῖ Jun 15 '14 at 21:54
  • Are you asking how many "segments" or runs of 72 floats you have once the complete file has been read into the vector? Isn't that just `vector.size() / 72`? Can the file contain some count of values that isn't a multiple of 72? – Blastfurnace Jun 15 '14 at 21:59
  • 2
    You know from your previous question that you cannot get the correct answer by measuring the size of the file. Are you saying that measuring the size of the vector gives a value that is "too large"? Could you tell us how many numbers are in the file, and what `ReplayBufferVector.size()` returns, and the value of `fileSizeVector`, and what you think it ought to be? (If that's too complicated, *simplify your example*.) – Beta Jun 15 '14 at 22:01
  • the total number of floats in the file is multiple of 72, i was asking about the total number of segments with 72 floating point numbers – sarath Jun 15 '14 at 22:03
  • @sarath: Isn't this a basic arithmetic question? Divide the number of floats in the vector by 72. – Blastfurnace Jun 15 '14 at 22:03
  • 1
    `vector::size()` isn't like `sizeof(...)`, it doesn't return the numbers of bytes that an object occupies in memory, it returns *the number of elements in the vector*. – Beta Jun 15 '14 at 22:10
  • 1
    @sarath This is very basic maths. The number of segments containing 72 `float` values is the total number of `float` values divided by 72. *ie* `ReplayBufferVector.size() / 72`. – paddy Jun 15 '14 at 22:35

1 Answers1

0

The easiest way is to count the number of floats.
Reserve the correct size of vector then read all the floats into the vector..

std::ifstream   file("fileName.txt";
std::size_t     size = std::distance(std::istream_iterator<float>(file),
                                     std::istream_iterator<float>());

file.close();
file.open("fileName.txt");
std::vector<float>  data(size);   // allocate enough space
                                  // to hold all the floats you need.
                                  // Normally I would use the iterators to 
                                  // construct the vector but because it is
                                  // so large I want to reserve the space
                                  // for it first.

// copy from the file to the vector.
std::copy(std::istream_iterator<float>(file),
          std::istream_iterator<float>()
          data.begin());

Or if you just want to read into a file:

std::ifstream      file("fileName.txt");
std::vector<float> data(std::istream_iterator<float>(file), 
                        std::istream_iterator<float>());
Martin York
  • 234,851
  • 74
  • 306
  • 532
  • Well true, works as well. But why complicate it more than necessary, an not read the vector directly? Is this solution supposed to be faster, because of preserving the `vector data` size before reading all of the values again? – πάντα ῥεῖ Jun 15 '14 at 23:43
  • 1
    @πάνταῥεῖ: Yep. But you need to measure to see if it actually improves load time. Remember if the vector runs out of room a re-size will cause the vector to duplicate its content and re-size (at 150%) of the original size. If you need to do this a lot then the expensive of the copy starts to add up. – Martin York Jun 16 '14 at 00:03
  • Hmm, actually I can't see any hint from (any of) the OP's question, we're talking about really big files. They were just stating it's somehow containing multiples of 72 values. Could be 72 values per (a few lines), or whatever. I don't think just having e.g. 72 values per say 100 lines, and a reasonably preset vector, would outweigh the read operations. But yes, this needs to be measured finally. – πάντα ῥεῖ Jun 16 '14 at 00:10
  • 1
    see: http://stackoverflow.com/q/23939232/14065 `which contains tens of thousands of floating point numbers separated by space` – Martin York Jun 16 '14 at 00:12
  • Oops! Yes, you're right! I wasn't aware the OP's working through this problem using SO almost 3 weeks now here ;) ... I somehow doubt, that this will stop due to any of our answers :/ ... – πάντα ῥεῖ Jun 16 '14 at 00:19
  • Also I doubt a bit the OP might get the essence of your answer (neither I think they'll get mine), and apply it to their use case. There were so many misses about the basic stuff ... – πάντα ῥεῖ Jun 16 '14 at 01:01