Converting a string vector to an int vector


I’m new to C++, migrating from Processing.

I have a data file that I am loading as a string, then pushing it into a vector. I am then trying to cast each indice as an int, into (an int) vector and running into all kinds of trouble (again, C++ newbie). I can do it successfully with a lone string variable to a lone int variable. Yet, when I try it on a vector, I get conversion issues or EXC_BAD_ACCESS. The issue could lie with how I’m going about it conceptually (again, Processing convert).

links’s I’m referencing:

my data.txt file is:


my .h is as follows:

int numb;
string str;
string temp;
vector data;
vector dataCast;

my .ccp is as follows:

void testApp::setup(){

ifstream fin; //declare file stream ofToDataPath(“data.txt”).c_str() );
while(fin != NULL){
getline(fin, str);
data = ofSplitString(str, “,”);

for(int i = 0; i<data.size()-1; i++){
temp = data[i];
stringstream convert(temp);
//istringstream (temp) >> numb;
if( !(convert >> temp)){
result =0;
dataCast[i] = temp;
//dataCast[i] = numb;


for(int i = 0; i <data.size()-1; i++){
dataCast[i]= stringToNumber(data[i]);


Any insights or ref links appreciated! Thank you!

Hello LadyK,

Have you tried using the function ofToInt(), from ofUtils: ?


I think this would work:

string var1 = “5”;
int var2 = 0;

var2 = (int) var1;

Not sure what happends when the var1 is not numeric tho.

Hi Meach,

Thanks for the direction to ofToInt()!! I somehow missed that when searching through the ofUtils page. :confused: It’s much more straight forward.

I still get EXC_BAD_ACCESS errors with the int vectors. I’m not sure why. When I use an int array, I can get it to work. But I’m not sure why it won’t work with vectors. The issue seems to be with the vector. What am I missing?Any ideas?

Jochem, I did try casting. I can’t get that to work with a vector either.

Here’s my code…

my .h file is as follows:

#define SLOTS 10

vector data;
vector dataCast;
//int dataCast[SLOTS];

my .cpp file is as follows:

void testApp::setup(){

ifstream fin; //declare file stream ofToDataPath(“data.txt”).c_str() ); //open text file
//vector data; //declare a vector of strings to save

while(fin!= NULL){ // as long as theres still text to read
//string str; //declare a string to store

getline(fin, str); //get a line from the file, put it in the string

data.push_back(str); //push the string into a vector of strings. ea line is new indice
data = ofSplitString(str, “,”); //splits into indice by , also subtracts comma

cout << data.size() << endl;

for(int i = 0; i < data.size(); i++){
dataCast[i] = 0;

for(int i = 0; i < data.size() - 1; i++){
dataCast[i] = ofToInt(data[i]);
cout << dataCast[i] << endl;

dataCast[1] = dataCast[2] + dataCast[1];
cout << dataCast[1] << endl;

Hey there,

maybe try the following:

	// ofBuffer is super helpful to deal with files. by default it treats a file as a text file,  
	// and by default it will also assume that the path is relative to your /data/ path, so there is no need to call  
	// ofToDataPath("data.txt")  
	ofBuffer tmpBuf = ofBufferFromFile("data.txt");  
	// we have the file contents in a buffer. now we want to have it in a string.  
	// ofBuffer knows how to cast to a string, and it will automatically do so if  
	// we assign our new buffer to a brand new string variable.  
	string contentsOfTextFileAsString = tmpBuf; // this will store the contents of the text file in a string  
	// note that we don't assume that there are any line breaks in the file.  
	// if there are line breaks in the file, you can do the following:  
	//	string contentsOfMultiLineTextFileAsStringWithLinebreaks = tmpBuf;  
	//	// the following will split our string at line breaks and then join it back together again, replacing line breaks with commas (",")  
	//	string contentsOfMultiLineTextFileAsStringWithoutLinebreaks = ofJoinString(ofSplitString(contentsOfMultiLineTextFileAsStringWithLinebreaks, "\n"), ",");  
	// contentsOfTextFileAsString = contentsOfMultiLineTextFileAsStringWithoutLinebreaks;  
	vector<int> numbersVector;	// this will hold the numbers  
	vector<string> numbersAsStrings = ofSplitString(contentsOfTextFileAsString, ",");  
	// now we push every number to the end of our integer vector  
	for (size_t i=0; i<numbersAsStrings.size(); i++){  
		int convertedNumber = ofToInt(numbersAsStrings[i]);  
		ofLogNotice() << convertedNumber;  
	// now we calculate the sum of all numbers:  
	int sumOfAllNumbers = 0;  
	for (size_t i = 0; i<numbersVector.size(); i++){  
		sumOfAllNumbers += numbersVector[i];  
	ofLogNotice() << "The sum of all numbers is: " << sumOfAllNumbers;  

if you are fresh to c++, one of the best books to get started is

“Accelerated C++, Practical Programming by Example” by Andrew Koenig & Barbara E. Moo.

It’s one of the books that helped me most to get over the initial scruffiness of the language =)



1 Like

I think what tgfrerer posted works.

But about the bad access error you had, I think it was related to the fact that your int vector is empty.
You need to push_back values to it as you were doing it for your string vector.

this is wrong:

for(int i = 0; i < data.size() - 1; i++){

dataCast = ofToInt( data );

should be:

for(int i = 0; i < data.size(); i++){

dataCast.push_back( ofToInt( data[i] ) );

You need to tell the code which part of data you want.
And I think you’re missing the last entry if you -1 the size, it will go on as long as its smaller than size() not <= equal and smaller.

tgfrerer, Meach and jochem,

thank you so much for your input and guidance! I now understand the issue (not pushing back into the vector). I’m still a bit new to using a vector.

tgfrerer - thanks so much for your insight about ofBuffer. This seems to be a more straightforward way to go. At least, it makes more sense to me. Thanks for the reading suggestion. I’m adding it to my list.

Again - thanks for your time and advice! Much appreciated! :slight_smile: