LinuxQuestions.org
Help answer threads with 0 replies.
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 10-26-2007, 06:03 PM   #1
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Rep: Reputation: 16
tokenize to put in a char* argv[] list


Hi,

I would like to have a function that would produce the same content has the main(int argc, char* argv[] ) method produces.

I don't have a clue in how to do it. Can anybody give me a hint?
The main problem, is that I don't know how to tokenize a string that could have a variable number of spaces that separate each word:

For example:
Code:
[4spaces]ls[2 spaces]-la[10 spaces]>out.txt[7spaces]

or

ls>out.txt
and put this values inside a char* argv[].


Thanks,
 
Old 10-26-2007, 06:06 PM   #2
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
Or have another command like:
Code:
cat out.txt | sort
 
Old 10-27-2007, 12:13 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Code:
string0 = "This is    a     string   \n";
To convert it to a series of strings, simply scan along the string and at each transition from whitespace to non-whitespace save a pointer to the character at that position. At each opposite transition, replace the whitespace character at that position with a '\0' null terminator.

At the end, you will have accumulated an array of pointers, and the original string will now look like:

Code:
string0 = "This\0is\0   a\0   string\0  \n";
argv[0] ---^     ^      ^     ^     
argv[1] ---------+      |     |
argv[2] ----------------+     |
argv[3] ----------------------+
argc = 4;
Get a pointer to the start of the string, an array of char *'s in which to collect your string pointers, and a counter of strings found. A couple of temporary variables to remember state at each point along the original string will alow you to detect the transition states. Use the isspace() function to determine the class of character at each point along the string.

Hope this helps.
--- rod.

Last edited by theNbomr; 10-27-2007 at 12:33 PM.
 
Old 10-28-2007, 07:22 AM   #4
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
This may do what you want; compile it, execute it, type a line of text and hit the return.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int     main    (void)
{
        char    buf [BUFSIZ];
        char    *tokptr, *strptr = buf;

        (void) gets (buf);
        while ((tokptr = strtok (strptr, " \t")) != (char *) NULL) {
                (void) fprintf (stdout, "%s\n", tokptr);
                strptr = (char *) NULL;
        }
        exit (EXIT_SUCCESS);
}
 
  


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
Help with cpp + tokenize + hashing kshkid Programming 2 12-29-2006 08:06 AM
Put Gentoo in the Distro List ctkroeker LQ Suggestions & Feedback 13 07-07-2005 06:38 PM
Tokenize a string in C++ russoue Linux - Software 2 01-11-2005 12:00 AM
main(int argc, char **argv) Longinus Programming 4 06-12-2004 07:22 AM
How i put news window managers in to the list of GDM or KDM? servimo Mandriva 0 05-20-2004 01:29 PM

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

All times are GMT -5. The time now is 07:19 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