Thursday, July 30, 2009

C++- How do i transfer a long string from file to an array?

I have a .txt file that looks more or less like this:





Name1 (number) (number) (number) Name2 (number) (number) (number) etc....





it is all on one line and each name and number has a space in between the value. I can insert the string into my C++ program but it is one long string. I would like to have it as an array of char or integers so i can use the numbers after each name. Or if anything just get an array of strings like :





string array[10000];


// the array needs to be big because the entries will grow by //one name each time i run my program. But i want to use the //data later. so i figure i can put it into an array of strings.


array[0] = "Name1";


array[1] = (number);


array[2] = (number);


.....and so forth.





i am under the impression that you can use the white spaces in the original string to split it up between each word. Is that right?


Could somebody help me or lead me in the right direction?

C++- How do i transfer a long string from file to an array?
yes, split on the spaces, here's some code...





StringSplit(string str, string delim, vector%26lt;string%26gt; results)


{


int cutAt;


while( (cutAt = str.find_first_of(delim)) != str.npos )


{


if(cutAt %26gt; 0)


{


results.push_back(str.substr(0,cutAt))...


}


str = str.substr(cutAt+1);


}


if(str.length() %26gt; 0)


{


results.push_back(str);


}


}


No comments:

Post a Comment