ifstream -> string -> vector<int>

Hello,

I am using an ifstream to read an array of integers and insert it into a vector. The input file is referred to as mFile, and mSize is an integer that is defined earlier in my code.

1
2
3
string *mData;
mData = new string[mSize+1];
getline(mFile, *mData, '}');


The above code creates a string of a dynamic size mSize+1, then copies the array from mFile to the string. This works fine! However, later in my code I need to go through mData with a for() loop and take all of the elements, separated by but not including the commas, and pass them to a vector<int>. See below:

1
2
3
4
5
6
7
8
9
10
string mValue;
for(int i = 0; i <= mSize; i++)
{	
	if(mData[i]!=",")
		mValue.append(*mData,i,1);
	else
	{	int iValue = atoi(mValue.c_str());
		mValue.clear();
		mVector.push_back(iValue);
}	}


I believe my problem comes up in the first if() statement. What I'm trying to do is run through every element in mData, take out the numbers and append them if they belong to a multi-digit integer in the array (for example, append 2 and 3 if the element in the array is 23), and throw out all the commas. However I can't figure out how to compare individual elements of mData to a single char (in this case, the if() statement tries to compare element i to a comma), most likely because of how I initialized mData. The code above simply takes all the data from mData, including the commas, appends it character by character to mValue, and after each character is appended, appends mValue so far to mVector.

My result is that mVector<int> contains "3", "3,", "3,2", "3,2,", "3,2,1", "3,2,1," etc. Which obviously does not work since commas are not integers (believe it or not).

Anyways, point is, I'm in way over my head and making things worse more than anything else ^_^' If anyone knows of an easier way to do this, or can perhaps decipher what I did wrong, any help or insight will be greatly appreciated.

Sadface,
R.Young
I don't think this..
1
2
3
string *mData;
mData = new string[mSize+1];
getline(mFile, *mData, '}');


... does what you think it does.

That will read from mFile up to the first '}' and put everything into the first std::string of your array.

*mData simply returns the first string in your array. So only that string gets the data from the file.

How you need to read the data from the file will depend on how it is formatted. But to read a list of numbers into an array of strings you will need a loop.

So it probably makes more sense to read your data directly into a std::vector rather than a string array as an intermediate.
Last edited on
By the way: Shouldn´t You compare it to ',' instead of ",", because it is a single character and not a string literal?...
Yes and yes! I followed your advice, stumbled through it for a few days, and came up with something that works fine. Basically it uses getline() to bring the data into a vector, then push_back every non-comma element into the final vector.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char[2] mValue;		// was mValue before, now "element Value Buffer"
int mSize;		// size of the vector for map array input
int mV; // mValue in integer form

...

vector<char> mData(mSize); // map Data buffer
mapFile.getline(&mData[0], mSize, '}');

for(int i = 0; i < mSize; i++)
{
	if(mData.at(i) != ','  &&  i < mSize-1) // also check if it's the end of the array
		mValue[d] = mData.at(i);
	else	// if the (last) element is ready
	{	mV = mValue[0]-'0';
		mVector.push_back(mV);
}	}


However, Incubbus, because I was dealing with the string mData (in my first post), I had to refer to the elements as string literals. I'm not sure why actually, when I put it in single quotations I got some error, and double quotes fixed that. Anyways, now that I'm referring to the vector<char> mData, I must refer to the elements as chars.
Topic archived. No new replies allowed.