LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-26-2004, 09:56 AM   #1
jpbarto
Senior Member
 
Registered: Mar 2003
Location: Pittsburgh, PA
Distribution: Gentoo / NetBSD
Posts: 1,251

Rep: Reputation: 45
Alternatives to strtok?


Ok, I am storing strings that contain '\n' in a char *. I need to print the strings one by one (delimted by the newline). I was using strtok but it clobbers my original copy of the data, replacing every newline with a null terminator. Any recommendations on how I 'untokenize' my original string? Would it be better to create a copy of the string? or is there a good alternative to strtok / strtok_r?

Thanks in advance,
jpbarto
 
Old 03-26-2004, 10:13 AM   #2
shortfuse
Member
 
Registered: Feb 2004
Location: Texas
Distribution: Debian
Posts: 30

Rep: Reputation: 15
Why not add the newline when you print the data?

printf("%s\n", token);

Wait now I see what your saying, yeah making a copy would probably be easiest since your gonna lose your delimiters also. I can't think of any alternatives that would be any more efficient and strtok is the most efficient way to parse delimitted strings.

Last edited by shortfuse; 03-26-2004 at 10:24 AM.
 
Old 03-26-2004, 10:30 AM   #3
jpbarto
Senior Member
 
Registered: Mar 2003
Location: Pittsburgh, PA
Distribution: Gentoo / NetBSD
Posts: 1,251

Original Poster
Rep: Reputation: 45
Alright, thanks. I'm displaying the string in Xlib (XDrawString... is there another way) and it doesn't print newlines... it just dumps everything onto one line. So I have to go through the carriage returns and print each one by one.

Thanks for the help,
jpbarto
 
Old 03-26-2004, 01:01 PM   #4
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
Quote:
or is there a good alternative to strtok / strtok_r?
Well there's strsep() but it has the same problem I've just copied the string when I need to not clobber it.
 
Old 03-26-2004, 01:20 PM   #5
jpbarto
Senior Member
 
Registered: Mar 2003
Location: Pittsburgh, PA
Distribution: Gentoo / NetBSD
Posts: 1,251

Original Poster
Rep: Reputation: 45
Thanks for the tips. I've gone with the copying and it is working out well.

jpbarto
 
  


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
C using strtok to parse by quotes? zero79 Programming 3 03-07-2005 03:44 PM
how to print the first character in a string using strtok its_godzilla Programming 5 02-02-2005 10:22 AM
strtok pantera Programming 2 12-19-2004 01:04 PM
free memory from strtok? swinchen Programming 1 09-08-2004 11:01 PM
strsep vs. strtok irfanhab Programming 1 05-01-2004 01:08 AM

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

All times are GMT -5. The time now is 08:20 PM.

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