Sponsored Content
Top Forums Programming Building an argc/argv style structure from a string (char*) Post 302380878 by jim mcnamara on Wednesday 16th of December 2009 12:27:00 PM
Old 12-16-2009
You need to parse your line of text into fields.

strtok() allows the use of multiple field delimiters. It has drawbacks, but you can call it
on a temporary string several times to get the field breakout you need.

You can also use lex/yacc to parse fields. An intermediate approach is to use the regex engine. See man regcmp and/or man regex.

Now - the obvious question - why can you not simply use argc, argv? ie., write a child process that is "called" with your string:

Code:
/* mychild.c */
#include <stdio.h>
int main( int argc, char **argv)
{
      int i=0;
      while(i<argc)
      {
           printf("argv[%d]=%s\n", i, argv[i++]);
       }
       printf("argc=%d\n", argc);
}

call your child code with popen -
Code:
void foo(char *stringtoparse)
{
char tmp[256]={0x0};
char cmdstr[256]={0x0};
FILE *cmd=NULL;

sprintf(cmdstr, "./mychild %s", stringtoparse);
cmd=popen(cmdstr, "r");
while(fgets(tmp, sizeof(tmp), cmd)!=NULL)
   printf("%s", tmp);
pclose(cmd);
}


Last edited by jim mcnamara; 12-16-2009 at 01:32 PM..
 

9 More Discussions You Might Find Interesting

1. Programming

Using argv argc

I searched on the forums. No advises. I am using a previous source code. I changed the main function main(int argc, char **argv) in a function misc(int argc, char **argv). How do you use the argc and argv parameters? This is how I am calling the function : char param; strcat(param,"wgrib ");... (4 Replies)
Discussion started by: Akeson Chihiro
4 Replies

2. Programming

dbx debugger + argv[argc]

Is it possible to use the dbx debugger with the CL options for the executable ? Say you have created a executable called myfunc which can take string arguments at run-time. You run it like this ./myfunc Hello World where Hello and World are the string arguments My question is whether... (1 Reply)
Discussion started by: JamesGoh
1 Replies

3. Programming

help for argv argc

Hi C experts, I have the following code for adding command line option for a program int main (argc, argv) int argc; char *argv; { char *mem_type; //memory type char *name; //name of the memory int addr; //address bits int data; ... (5 Replies)
Discussion started by: return_user
5 Replies

4. Programming

How to turn argv[1] into a string in C?

i have a function that's parameter is char *s and in the main function i am sending that function &(argv), but i dont think this is working, how can i fix this? can i cast it to be a string or something? is there a way i can create a new string thats exactly what argv is equal to... (6 Replies)
Discussion started by: omega666
6 Replies

5. Shell Programming and Scripting

ARGV and ARGC in bash 3 and bash 3.2

Hi Folks, I've prepared a shell script that takes action based on arguments and number of arguments..sample code like: ARGV=("$@") ARGC=("$#") case ${ARGV} in abc) if ; then ...... else printf "\nInvalid number of arguments, please check the inputs and... (2 Replies)
Discussion started by: SBC
2 Replies

6. Shell Programming and Scripting

argc/ argv in awk

Hi guys, i'm trying to solve this problem. I have to run something like cat file1.txt | awk -f script.awk 10 if i'm in the awk script, how can i take the parameter :10 ??:wall: i try something like : BEGIN{ var=argv } {..} END{..} but obviously is not correct... (5 Replies)
Discussion started by: heaven25
5 Replies

7. UNIX for Advanced & Expert Users

O argv, argv, wherefore art thou argv?

All of my machines (various open source derivatives on x86 and amd64) store argv above the stack (at a higher memory address). I am curious to learn if any systems store argv below the stack (at a lower memory address). I am particularly interested in proprietary Unices, such as Solaris, HP-UX,... (9 Replies)
Discussion started by: alister
9 Replies

8. Programming

Building Block style programming Book

Hello to all, Here is my situation. Some time in the mid-80's I stumbled across a small white programming book - can't remember the name but it was unique in that it started right out giving instructions on creating building blocks in code as a foundation for a complete system. The book was... (2 Replies)
Discussion started by: jozefn
2 Replies

9. Homework & Coursework Questions

Help using argc/argv in assignment

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: First, create a "hello world" program that prints "Hello World". But NOW, instead use argc to verify that a... (9 Replies)
Discussion started by: miniviking10
9 Replies
aa_parseoptions(3)					     Library Functions Manual						aa_parseoptions(3)

NAME
aa_parseoptions - parse the standard command line options used by AA-lib. SYNOPSIS
#include <aalib.h> int aa_parseoptions ( struct aa_hardware_params *p, aa_renderparams *r, int *argc, char **argv ); PARAMETERS
struct aa_hardware_params *p Hardware parameters structure to alter. It is expected that this structure only with necessary modifications will be later used to initialize the AA-lib context. aa_renderparams *r Rendering prameters structure to alter. It is expected that this structure only with necessary modifications will be later used to render images. int *argc Pointer to argc parameter passed to function "main". char **argv Pointer to argv parameter passed to function "main". DESCRIPTION
Use this function to parse the standard command line options used by AA-lib. Every AA-lib program ought to call this function to let user specify some extra parameters. The function alters the aa_hardware_params and aa_renderparams structures and removes known options from the argc/argv lists. It also parse the AAOPTS environment variable. When called with NULL for the argc/argv parameters, it parses AAOPTS only. At least this call ought to be in every AA-lib program. RETURNS
1 when sucesfull and 0 on failure. The program then can print the help text available in aa_help variable. SEE ALSO
save_d(3), mem_d(3), aa_help(3), aa_formats(3), aa_fonts(3), aa_dithernames(3), aa_drivers(3), aa_kbddrivers(3), aa_mousedrivers(3), aa_kbdrecommended(3), aa_mouserecommended(3), aa_displayrecommended(3), aa_defparams(3), aa_defrenderparams(3), aa_scrwidth(3), aa_scrheight(3), aa_mmwidth(3), aa_mmheight(3), aa_imgwidth(3), aa_imgheight(3), aa_image(3), aa_text(3), aa_attrs(3), aa_currentfont(3), aa_autoinit(3), aa_autoinitkbd(3), aa_autoinitmouse(3), aa_recommendhi(3), aa_recommendlow(3), aa_init(3), aa_initkbd(3), aa_initmouse(3), aa_close(3), aa_uninitkbd(3), aa_uninitmouse(3), aa_fastrender(3), aa_render(3), aa_puts(3), aa_printf(3), aa_gotoxy(3), aa_hidecursor(3), aa_showcursor(3), aa_getmouse(3), aa_hidemouse(3), aa_showmouse(3), aa_registerfont(3), aa_setsupported(3), aa_setfont(3), aa_getevent(3), aa_getkey(3), aa_resize(3), aa_resizehandler(3), aa_edit(3), aa_createedit(3), aa_editkey(3), aa_putpixel(3), aa_recommendhikbd(3), aa_rec- ommendlowkbd(3), aa_recommendhimouse(3), aa_recommendlowmouse(3), aa_recommendhidisplay(3), aa_recommendlowdisplay(3) c2man aalib.h 8 September 1999 aa_parseoptions(3)
All times are GMT -4. The time now is 04:29 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy