Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading between the commas

Status
Not open for further replies.

croc

Technical User
Jan 8, 2002
12
0
0
GB
I want to take information between commas from a big string and copy bits of information to smaller strings
e.g.
string x1("cow,dog,sheep,horse,goat,snake,pig,rooster,ant")

I need to put cow into string x2, sheep into string x3 etc.

So esentially i want to read info between the commas.
Then when i have finished with the values in x2 and x3 i want to replace them with other values, say snake and rooster.

Any ideas people???
 
It's all indexOf, substring, and concatonations. I'm not sure exactly what you wish to do but if you want I can send up some sample code. If not, it's simply truncating everythign after what you want for the begining, concatonating what you wish to replace, and then concatonating everything after what you didn't want. Pretty simple string manipulation. Good luck! MYenigmaSELF:-9
myenigmaself@yahoo.com
"If debugging is the process of removing bugs, then programming must be the process of putting them in." --Dykstra
 
char *Ptr = "cow,dog,sheep,horse,goat,snake,pig,rooster,ant";
char *temp;

temp = strtok(Ptr,",");
while(temp)
{
printf("%s\n", temp);
temp = strtok(NULL, ",");
};
 
Ahh, tanks, can you tell me what %s does/means in that bit of code.
Also, how would i make that code, instead of printing to the screen, which i am assuming it does, save to an array.
By the way thanks, that code is pretty much what I was looking for.
 
What i want to do, i have shed loads of values seperated by commas. Every four values is a group so i would like to take the values from the file, seperate them from the commas and put them in an arry shaped like this:

string file("a1,a2,a3,a4,b1,b2,b3,b4,c1,c2,c3,c4,etc");

const char array[]={{a1,a2,a3,a4},
{b1,b2,b3,b4},
{c1,c2,c3,c4},
{etc}};
This is what I am trying to do, can i do this using strtok or is another way better.
 
printf formats the string in the respect that it puts the first variable after the string into the first %x after the string. %s indicates that it is a string value. %d would represent a decimal integer and so on. to put it into an array simply use a for loop instead of a while, and iterate through the array populating it as necessary.
MYenigmaSELF:-9
myenigmaself@yahoo.com
"If debugging is the process of removing bugs, then programming must be the process of putting them in." --Dykstra
 
#include <string>

void main ()
{
std::string file(&quot;a1,a2,a3,a4,b1,b2,b3,b4,c1,c2,c3,c4,etc&quot;);

std::string Strs[20];
char *temp;

temp = strtok((char *)file.c_str(),&quot;,&quot;);
int i =0;
while(temp)
{
printf(&quot;%s\n&quot;, temp);
Strs = temp;
i++;
temp = strtok(NULL, &quot;,&quot;);
};
}
 
hi how do i go abouts storing values into a multi-dimensional array and then reading off this array, i would appreciate it if someone could help me out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top