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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strtok tokenise a string problem 1

Status
Not open for further replies.

vintl

Technical User
Jun 9, 2000
72
0
0
MY
i want to strtok a string from flat file which consist of 3 part..(eg. N XXX98 34-XXX)
howto assign each part into a variable so i can memcpy the last part into a variable? i do not want to use printf which print out the output to screen.
 
If you dont want to use "printf", you can still use "sprintf".

Code:
example: char buffer[30] = {0};
         sprintf( buffer, "Hello world !.\n" );
 
Take a look at your compilers docs for:
fscanf()
sprintf()
 

string is from the file
string2 is a char * declared in the code

x = strlen (string);
int z = 0;

for (int y = 0; y < x; y++)
{
if (Z == 2)
{
for (z = 0; y < x; y++)
{
string2 [z] = string [x];
z++;
}
string2 [z] = NULL;
}
else if (string [y] == ' ')
z++;
}

this may need tweaking but its the method I'd
use to get the last parameter in the string gotten
from the file. you can modify it to get each parameter.

tomcruz.net
 
You could also make use of the [tt]strrchr[/tt] function. Something like this:
[tt]char *last;
last = strrchr(string, &quot; &quot;);
last += sizeof(char);[/tt] //Daniel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top