LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   parsing strings problem (https://www.linuxquestions.org/questions/programming-9/parsing-strings-problem-171506/)

McB 04-17-2004 09:50 PM

parsing strings problem
 
Hi.

A little question someone might be able to help with regarding parsing strings.

ive got a little function that takes a string in the format of a phone number, a space, and a first name.

something like:
char buffer[] = "12345678 john".

i need to break the string into the number and the name.
the code i have which does that, and converts the number into an int is

Code:

  char buffer[] = "123456 bob"
  char *num, *name;
  int number

  num = strtok(buffer," ");
  name = strtok(NULL," ");
 
  number = atoi(num);

so i do a simple:
printf("Phone number is: %d name is %s", number, name)
to just check that it has actually been split into the 2 parts correctly. and sure enough, i get output like:
"phone number is 123456 name is bob".

BUT... if im passing 'buffer' into the function as a parameter i get problems.
my code is exactly the same except instead of declaring buffer to = '123456 bob' inside the function like the code above, im passing it in. EG

void parseFunc (char buffer[])

thats the only change in the code, yet when i run this version the output i get is:
"phone number is 123456 name is (null)".

what am i doing wrong? why do i get a 'null" value when i parse a string thats been passed in, and not when i declare a string within the function?

any help would be much appreciated! Thanks

nodger 04-17-2004 10:40 PM

if you try to work on the string 'burrer' you'll run into problems because its static.

nodger 04-17-2004 10:47 PM

another word of advise: never use the printf function to print strings (%s). always use puts() or something that doesnt attempt to format it.

McB 04-18-2004 01:32 AM

Thanks for the puts vs printf tip.

so how do i go about parsing a string thats entered by the user, or passed in, as oppsed to the case above where its declared as already containing....something?

itsme86 04-19-2004 09:03 AM

You have to do 2 things differently:

1) declare your variables as arrays (char num[6], name[20]).
2) Use strcpy() to store strtok()'s results (strcpy(num, strtok(buffer, " ");)

That should fix all your problems.

kooch 04-19-2004 09:14 AM

Quote:

1) declare your variables as arrays (char num[6], name[20]).
Most definately does not need to be done. Why would you want to impose artificial limits unneccessarily?

If you already know the format of the input why not use the appropriate scanf() function?

itsme86 04-19-2004 09:50 AM

Quote:

Originally posted by kooch
Most definately does not need to be done. Why would you want to impose artificial limits unneccessarily?

If you already know the format of the input why not use the appropriate scanf() function?

The name, I'm guessing, is of variable length. Ideally he'd want to dynamically allocate the memory for the name and num, but all I was trying to do was get the point across, which is, you have to store strtok()'s result in your own buffer if you want to use the value after subsequent strtok() calls.

Even with your scanf() idea he'd have to store the results, be it by introducing "artificial limits" or by dynamically allocating the memory.


All times are GMT -5. The time now is 04:01 AM.