LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-17-2004, 09:50 PM   #1
McB
LQ Newbie
 
Registered: Feb 2004
Location: Australia
Posts: 6

Rep: Reputation: 0
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
 
Old 04-17-2004, 10:40 PM   #2
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Rep: Reputation: 30
if you try to work on the string 'burrer' you'll run into problems because its static.
 
Old 04-17-2004, 10:47 PM   #3
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Rep: Reputation: 30
another word of advise: never use the printf function to print strings (%s). always use puts() or something that doesnt attempt to format it.
 
Old 04-18-2004, 01:32 AM   #4
McB
LQ Newbie
 
Registered: Feb 2004
Location: Australia
Posts: 6

Original Poster
Rep: Reputation: 0
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?
 
Old 04-19-2004, 09:03 AM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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.
 
Old 04-19-2004, 09:14 AM   #6
kooch
Member
 
Registered: Mar 2004
Location: Upstate NY
Distribution: Slackware/YDL
Posts: 77

Rep: Reputation: 15
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?
 
Old 04-19-2004, 09:50 AM   #7
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
Problem the Strings in C++ gjagadish Programming 4 10-15-2005 03:45 AM
php problem with strings graziano1968 Programming 5 10-07-2005 02:10 PM
Problem in C: arrays, functions and strings OrganicX Programming 15 03-18-2003 09:31 AM
Problem with strings in Red Hat 8.0 smriti Linux - General 1 03-03-2003 07:59 AM


All times are GMT -5. The time now is 05:34 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration