parsing "path" from arvg[1] 
Author Message
 parsing "path" from arvg[1]

Language: ANSI C / VMS ANSI C

Hi guys,

I need a hand extracting the parts of a file path from the command line.

Assuming argv[1] was something like "C:\bin\images\image.dat"

I want to extract "C:\bin\images\" to var path, and "image" to var
fname, and finally "dat" to suffix.  I am stuck with something like:

int numfound;
char path[256],  fname[256], suffix[256];
numfound = sscanf (argv[1]," %[^\\]\ %[^.]. %s", path, fname, suffix);
if (numfound != 3) some_error_check_msg;

The problem is that the first %[^\\]\ only reads up to the first "\"
found in the string, and does not account for sub dirs in the path.
What I'd want in this example is c:\bin\images, but I'd want it genric
enough to parse c:\bin\images\sat_dat\new\image4.dat and extract the ALL
the parts of the path...?  
ie: - scan up to the last "\" and save that in path.
    - from that point in the string, scan up to, but not including the
"."
      to get the file name (then skip the "." = %[^.]. )
    - from that point, after the last "." scan the remainder (%s) into
      suffix.

Using ANSI C I assume there is no EASY way to do this, but I can still
pray!

Thanks for any help guys,
-Don



Sat, 02 Oct 1999 03:00:00 GMT  
 parsing "path" from arvg[1]

|> Language: ANSI C / VMS ANSI C
|>
|> Hi guys,
|>
|> I need a hand extracting the parts of a file path from the command line.
|>
|> Assuming argv[1] was something like "C:\bin\images\image.dat"
|>
|> I want to extract "C:\bin\images\" to var path, and "image" to var
|> fname, and finally "dat" to suffix.  I am stuck with something like:
|>
|> int numfound;
|> char path[256],  fname[256], suffix[256];
|> numfound = sscanf (argv[1]," %[^\\]\ %[^.]. %s", path, fname, suffix);
|> if (numfound != 3) some_error_check_msg;
|>
|> The problem is that the first %[^\\]\ only reads up to the first "\"
|> found in the string, and does not account for sub dirs in the path.
|> What I'd want in this example is c:\bin\images, but I'd want it genric
|> enough to parse c:\bin\images\sat_dat\new\image4.dat and extract the ALL
|> the parts of the path...?  
|> ie: - scan up to the last "\" and save that in path.
|>     - from that point in the string, scan up to, but not including the
|> "."
|>       to get the file name (then skip the "." = %[^.]. )
|>     - from that point, after the last "." scan the remainder (%s) into
|>       suffix.
|>
|> Using ANSI C I assume there is no EASY way to do this, but I can still
|> pray!
|>

  It's not really very difficult at all -- particularly if you give up
  the notion of using 'sscanf()'. How about:

  #include <string.h>

  void decipher_path(char *path,char *prefix,char *name,char *suffix)
  {
        char *p;

        p = strrchr(path,'\\');
        if ( p ) {      /* have directories and the like */
                *p = '\0';
                strcpy(path,prefix);
                *p = '\\';
                ++p;
        } else {        /* just a filename & possible suffix */
                *prefix = '\0';
                p = path;
        }
        q = strchr(p,'.');
        if ( q ) {      /* got a suffix */
                *q = '\0';
                strcpy(name,p);
                *q = '.';
                strcpy(suffix,++q);
        } else {        /* no suffix -- just a name [possibly] */
                strcpy(name,p);
                *suffix = '\0';
        }
   }

   looks like it might work pretty well. Of course, it assumes that its
   caller has sufficient space allocated to hold the various substrings;
   you might prefer to allocate them dynamically and rely on the caller
   to free them when no longer needed ...

|> Thanks for any help guys,
|> -Don

--
 Ed Hook                              |       Copula eam, se non posit
 Computer Sciences Corporation        |         acceptera jocularum.
 NASA Langley Research Center         | Me? Speak for my employer?...<*snort*>



Sat, 02 Oct 1999 03:00:00 GMT  
 parsing "path" from arvg[1]

Quote:

> Language: ANSI C / VMS ANSI C

> Hi guys,

> I need a hand extracting the parts of a file path from the command line.

> Assuming argv[1] was something like "C:\bin\images\image.dat"

> I want to extract "C:\bin\images\" to var path, and "image" to var
> fname, and finally "dat" to suffix.  I am stuck with something like:

> int numfound;
> char path[256],  fname[256], suffix[256];
> numfound = sscanf (argv[1]," %[^\\]\ %[^.]. %s", path, fname, suffix);
> if (numfound != 3) some_error_check_msg;

> The problem is that the first %[^\\]\ only reads up to the first "\"
> found in the string, and does not account for sub dirs in the path.
> What I'd want in this example is c:\bin\images, but I'd want it genric
> enough to parse c:\bin\images\sat_dat\new\image4.dat and extract the ALL
> the parts of the path...?
> ie: - scan up to the last "\" and save that in path.
>     - from that point in the string, scan up to, but not including the
> "."
>       to get the file name (then skip the "." = %[^.]. )
>     - from that point, after the last "." scan the remainder (%s) into
>       suffix.

> Using ANSI C I assume there is no EASY way to do this, but I can still
> pray!

No supplications to the divine will be necessary if you
throw away the sscanf(), copy argv[1] to a buffer, and parse it
using the string search functions in <string.h>.  Hint: do it
backwards using strrchr().  sscanf() is really meant for dealing
with relatively fixed format data input, not string parsing.

sue



Sun, 03 Oct 1999 03:00:00 GMT  
 parsing "path" from arvg[1]

Quote:

> Language: ANSI C / VMS ANSI C

> Hi guys,

> I need a hand extracting the parts of a file path from the command line.

> Assuming argv[1] was something like "C:\bin\images\image.dat"

> I want to extract "C:\bin\images\" to var path, and "image" to var
> fname, and finally "dat" to suffix.  I am stuck with something like:

> int numfound;
> char path[256],  fname[256], suffix[256];
> numfound = sscanf (argv[1]," %[^\\]\ %[^.]. %s", path, fname, suffix);
> if (numfound != 3) some_error_check_msg;

> The problem is that the first %[^\\]\ only reads up to the first "\"
> found in the string, and does not account for sub dirs in the path.
> What I'd want in this example is c:\bin\images, but I'd want it genric
> enough to parse c:\bin\images\sat_dat\new\image4.dat and extract the ALL
> the parts of the path...?
> ie: - scan up to the last "\" and save that in path.
>     - from that point in the string, scan up to, but not including the
> "."
>       to get the file name (then skip the "." = %[^.]. )
>     - from that point, after the last "." scan the remainder (%s) into
>       suffix.

> Using ANSI C I assume there is no EASY way to do this, but I can still
> pray!

Relax ! Lean back, start thinking nice thoughts for some minutes and
then
look at the functions in <string.h>. That should make you happy and it
should all be purest ANSI-C !

Especially functions like "strchr()" or "strtok()" (a little tricky to
use) or "strcspn()" should be very helpfull to you.

Stephan



Sun, 03 Oct 1999 03:00:00 GMT  
 parsing "path" from arvg[1]

Quote:


> > Language: ANSI C / VMS ANSI C

> > Hi guys,

> > I need a hand extracting the parts of a file path from the command line.

> > Assuming argv[1] was something like "C:\bin\images\image.dat"

> > I want to extract "C:\bin\images\" to var path, and "image" to var
> > fname, and finally "dat" to suffix.  I am stuck with something like:

> > int numfound;
> > char path[256],  fname[256], suffix[256];
> > numfound = sscanf (argv[1]," %[^\\]\ %[^.]. %s", path, fname, suffix);
> > if (numfound != 3) some_error_check_msg;

> > The problem is that the first %[^\\]\ only reads up to the first "\"
> > found in the string, and does not account for sub dirs in the path.
> > What I'd want in this example is c:\bin\images, but I'd want it genric
> > enough to parse c:\bin\images\sat_dat\new\image4.dat and extract the ALL
> > the parts of the path...?
> > ie: - scan up to the last "\" and save that in path.
> >     - from that point in the string, scan up to, but not including the
> > "."
> >       to get the file name (then skip the "." = %[^.]. )
> >     - from that point, after the last "." scan the remainder (%s) into
> >       suffix.

> > Using ANSI C I assume there is no EASY way to do this, but I can still
> > pray!

> Relax ! Lean back, start thinking nice thoughts for some minutes and
> then
> look at the functions in <string.h>. That should make you happy and it
> should all be purest ANSI-C !

> Especially functions like "strchr()" or "strtok()" (a little tricky to
> use) or "strcspn()" should be very helpfull to you.

...in what way to you find "strtok" 'tricky to use'....compared to
"scanf" it's trivial......is "scanf" the ugliest....function is
standard C......it get's the thumbs down from me everytime....

G.

Quote:

> Stephan

....message ends.....


Sun, 10 Oct 1999 03:00:00 GMT  
 parsing "path" from arvg[1]

 > Language: ANSI C / VMS ANSI C

Quote:

 > Hi guys,

 > I need a hand extracting the parts of a file path from the command line.
 > Assuming argv[1] was something like "C:\bin\images\image.dat"

Quote:

> I want to extract "C:\bin\images\" to var path, and "image" to var
> fname, and finally "dat" to suffix.  

This should be very easy; you should definitely learn how to use strtok, or
even to write your version of this function. Otherwise, don't try to use C or
any other serious programming language.
An easy way to solve your problem will be to copy your string to one char array,
to scan it (backwards), and to replace the first \ with \0.
Simultaneously you can find pointer to image.dat part of your string, etc...


Sun, 10 Oct 1999 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Parse trees and "("")"

2. parsing error: expected ")".

3. "Parsing the Commandline"-question

4. Difficulty with the "include path"

5. PATH used during "post-Build" rules

6. remove() vrs fopen("""w")

7. Displaying binary data as ascii "1"'s and "0"'s

8. Looking for "Shroud"/"Obfus"

9. ""help with TSR""

10. Error "free"-ing "malloc"-ed memory

11. Displaying binary data as ascii "1"'s and "0"'s

12. Help: Debugging parse errors about "{" or "}".

 

 
Powered by phpBB® Forum Software