How to write vector data into a file.

I need to write the contents of 4 vectors into a file. I need each vector written into a new line.
For instance I have a code vector, a description, a vector for how many of each item, and a vector to hold the price. So i need each vector in a different line

10001
diet coke
12
1.00

Do i have to make the contents of the vector into strings first, to be able to copy them to a file? Because I haven't been able to write straight from the vector.
And then to separate them do I output it line by line?
Here's my function... So I made new string variables to make the vector's content into strings.

I included my vector names commented out.

thanks for looking at my code.
If i need to include anymore pieces of code let me know.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  void Interface::saveToDisk(string fileName)
{
    string codeOut;
    string itemOut;
    string descriptionOut;
    string priceOut;

    ofstream outputFile;
    outputFile.open (fileName.c_str());

    /*vector <int> myCode;
    vector <string> myDescription;
    vector <int> myItem;
    vector <double> myPrice;
    */
    
    for (int i=0; i < myCode.size(); i++)
    {
        outputFile << codeOut << endl;
        outputFile << descriptionOut << endl;
        outputFile << itemOut << endl;
        outputFile << priceOut << endl;
    }

    outputFile.close();
}
You should be able to use the for loop you already have, and just index through the vectors, outputting each element into the file.
1
2
3
4
5
6
7
for (x=.....) {
  out<<vectorA[x]<<endl;
  out<<vectorB[x]<<endl;
  .
  .
  .
}
I had that before, and I was messing around with turning them to strings, and all sorts of things.

I managed to fix it by making the program ask for the file name in which it needed to save it to. I was opening and closing the outputfile going to nowhere. XD.

Thanks for confirming my code though.
Topic archived. No new replies allowed.