Writing a C++ vector to an output file

13,800

Solution 1

First, you shouldn't call your variable vector. Give it a name which is not the name of a class from the Standard Library.

Secondly, ostream_iterator will append a ',' even after the last element of the vector, which may not be what you want (a separator should be a separator, and there's nothing to separate the last value of the vector from a further value).

In C++11, you could use a simple range-based for loop:

outputFile << "GLfloat vector[]={";
auto first = true;
for (float f : v) 
{ 
    if (!first) { outputFile << ","; } 
    first = false; 
    outputFile << f; 
}
outputFile << "}" << endl;

In C++03, it is going to be just a bit more verbose:

outputFile << "GLfloat vector[]={";
auto first = true;
for (vector<float>::iterator i = v.begin(); i != end(); ++i) 
{ 
    if (!first) { outputFile << ","; c++; } 
    first = false;
    outputFile << *i;
}
outputFile << "}" << endl;

Solution 2

You've taken the solution and attempted to stick it into the stream insertion. That's not how it works. It should be a separate line:

outputFile << "GLfloat vector[]={";
copy(vector.begin(), vector.end(), ostream_iterator<float>(outputFile, ", "));
outputFile << "}" << endl;

The copy algorithm simply copies elements from one range to another. ostream_iterator is a special iterator that will actually insert (with <<) into the given stream when you do *it = item_to_insert;.

Share:
13,800
user2136754
Author by

user2136754

Updated on June 13, 2022

Comments

  • user2136754
    user2136754 almost 2 years
    ofstream outputFile ("output.txt");
    
    if (outputFile.is_open())
    {
         outputFile << "GLfloat vector[]={" <<  copy(vector.begin(), vector.end(), ostream_iterator<float>(cout, ", ")); << "}" << endl;
    }
    else cout << "Unable to open output file";
    

    How do I output a vector to a file, with each float separated by commas? I would also like to avoid printing square brackets if possible.

  • user2136754
    user2136754 about 11 years
    It still says copy_n is not declared, despite including all the following: #include <iostream> #include <fstream> #include <string> #include <vector> #include <iterator> #include <algorithm> #include <stdlib.h>
  • David G
    David G over 10 years
    Think you got that for loop mixed up.
  • David G
    David G over 10 years
    No problem. Is it okay if I ask: Why haven't you been answering questions lately?
  • Andy Prowl
    Andy Prowl over 10 years
    @0x499602D2: I have a lot of work to do and some health issues that forced me to re-prioritize my activity on SO, but I plan to get back soon :)
  • David G
    David G over 10 years
    Best of luck. I hope everything gets better for you. :)
  • Andy Prowl
    Andy Prowl over 10 years
    @0x499602D2: Thank you, I appreciate it :)
  • aatish
    aatish over 9 years
    @AndyProwl Shouldn't the 'c++' be outside of the if block? If not, c would never increase from 0. Also, perhaps a bool would be a better option than using an int.
  • Andy Prowl
    Andy Prowl over 9 years
    @user19448: Indeed, that was an oversight. I edited the answer, thank you.