Converting a string to an integer array

I am trying to convert a string of numbers to an integer array. Each number is a single digit so I should be able to just iterate over the characters and store them to an array. The code I have so far is:
1
2
3
4
5
6
7
8
9
10
11
12
    string stringSaveFileData;
    fstream saveFile ("save.txt", fstream::in | fstream::out);
    getline(saveFile, stringSaveFileData);
    saveFile.close();

    stringstream convert ();
    vector<int> numericSaveFileData;
    for (unsigned int i=0; i < stringSaveFileData.length(); i++)
    {


    }


I was thinking I would use stringstream to convert it but I can only get it to convert a string to a single integer, so I would recieve 123456 for example, but I want 1,2,3,4,5,6.
You could read one character at a time into the stringstream (using string::at()), then output the resulting integer from the stringstream into your vector.

-Albatross
I am getting an error when I try to run the program with what I think you are suggesting:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream> // Standard input and output
#include <fstream> // Reading to and from files
#include <vector> // Allows for dynamic array resizing
#include <string> // Allows for reading from the savefile
#include <sstream> // Allows for processing the savefile string into a vector
using namespace std;


int main()
{
    string stringSaveFileData;
    fstream saveFile ("save.txt", fstream::in | fstream::out);
    getline(saveFile, stringSaveFileData);
    saveFile.close();

    stringstream convert;
    vector<int> numericSaveFileData;
    for (unsigned int i=0; i < stringSaveFileData.length(); i++)
    {
        numericSaveFileData[i] << convert(stringSaveFileData.at(i));
    }
}
Try numericSaveFileData[i] = stringSaveFileData[i] - '0';
The process is returning a -1 when it reaches that line of code.
Ah, right. You have to set your vector's size too.

vector<int> numericSaveFileData(stringSaveFileData.length());
Thanks a bunch!
Topic archived. No new replies allowed.