LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 03-07-2005, 12:46 PM   #1
zero79
Member
 
Registered: Nov 2003
Location: Ohio
Distribution: Debian Unstable
Posts: 460

Rep: Reputation: 30
C using strtok to parse by quotes?


i'm trying to use 'strtok' to process out a name that may contain spaces that is sandwiched between quotes. i can't seem to correctly parse out the name. here is my code thus far:

Code:
#include <stdio.h>

int main(void)
{
  char line[] = "53      \"example name\"";
  char *num, *name;
  
  printf("%s\n", line);
  
  num = (char *)strtok(line, " ");
  name = (char *)strtok(NULL, "\"");
  
  printf("num = %s, name = %s\n", num, name);
}
the output when executing the code is

Code:
num = 53, name =
i want the output to be

Code:
num = 53, name = example name
what am i doing wrong, and how can a i retrieve the data i am looking for? thanks for any thoughts.
 
Old 03-07-2005, 12:55 PM   #2
zero79
Member
 
Registered: Nov 2003
Location: Ohio
Distribution: Debian Unstable
Posts: 460

Original Poster
Rep: Reputation: 30
ok, answering my own question. i should parse by the quote character first, then by spaces. this code does what i expect.

Code:
#include <stdio.h>

int main(void)
{
  char line[] = "53   \"example name\"";
  char *num, *name, *tmp;
  
  printf("%s\n", line);
  
  tmp = (char *)strtok(line, "\"");
  name = (char *)strtok(NULL, "\"");
  num = (char *)strtok(tmp, " ");
  
  printf("num = %s, name = %s\n", num, name);
}
peace.
 
Old 03-07-2005, 01:06 PM   #3
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
I like it better with strchr()
Code:
itsme@dreams:~/C$ cat getname.c
#include <stdio.h>
#include <string.h>

int main(void)
{
  char line[] = "53      \"example name\"";
  char *num, *name;

  num = line;
  name = strchr(num, ' ');
  *name = '\0';
  name = strchr(name+1, '"')+1;
  *strchr(name+1, '"') = '\0';;

  printf("num = '%s', name = '%s'\n", num, name);
  return 0;
}
Code:
itsme@dreams:~/C$ ./getname
num = '53', name = 'example name'
itsme@dreams:~/C$
 
Old 03-07-2005, 03:44 PM   #4
zero79
Member
 
Registered: Nov 2003
Location: Ohio
Distribution: Debian Unstable
Posts: 460

Original Poster
Rep: Reputation: 30
yeah, nice bit of trickiness. fun stuff. peace.
 
  


Reply



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
Problems with quotes and double quotes Andruha Slackware 6 01-02-2010 04:44 PM
Using single quotes vs double quotes in PHP strings vharishankar Programming 6 07-11-2005 11:41 AM
strtok pantera Programming 2 12-19-2004 01:04 PM
strsep vs. strtok irfanhab Programming 1 05-01-2004 01:08 AM
Alternatives to strtok? jpbarto Programming 4 03-26-2004 01:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:36 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration