command line arguements with spaces in them

Discussion in 'C Programming' started by raphfrk@netscape.net, Jul 14, 2006.

  1. Guest

    I have a program which reads in 3 filenames from the command line

    prog filename1 filename2 filename3

    However, it doesn't work when one of the filenames has spaces in it
    (due to a directory name with a space in it) because that filename gets
    split into 2.

    I tried

    prog "/blah/blah 2/filename1" "filename2" "filename3"

    and it still splits on the space. (It included the " chars too).

    Is there a quick fix that would be possible here? I realise that it
    could be fixed by manually detecting the " and creating the 3
    filenames. However, if there was a way to write the command line so
    that isn't necessary, that would be better/easier.
     
    , Jul 14, 2006
    #1
    1. Advertising

  2. In article <>,
    <> wrote:
    >I have a program which reads in 3 filenames from the command line
    >
    >prog filename1 filename2 filename3
    >
    >However, it doesn't work when one of the filenames has spaces in it
    >(due to a directory name with a space in it) because that filename gets
    >split into 2.
    >
    >I tried
    >
    >prog "/blah/blah 2/filename1" "filename2" "filename3"


    The quotation marks are interpreted by the shell before the
    arguments are passed to the program. What you have written
    above will work on most shells under Unix. Alternatively,
    you may protect the space by a backslash, as in:

    prog /blah/blah\ 2/filename1 filename2 filename

    Something like that may or may not work on other systems.
    You may want to ask your question in a newsgroup dedicated
    to your system.

    --
    Rouben Rostamian
     
    Rouben Rostamian, Jul 14, 2006
    #2
    1. Advertising

  3. said:

    > I have a program which reads in 3 filenames from the command line
    >
    > prog filename1 filename2 filename3
    >
    > However, it doesn't work when one of the filenames has spaces in it
    > (due to a directory name with a space in it) because that filename gets
    > split into 2.
    >
    > I tried
    >
    > prog "/blah/blah 2/filename1" "filename2" "filename3"
    >
    > and it still splits on the space. (It included the " chars too).
    >
    > Is there a quick fix that would be possible here?


    Yes. Rename the file so that it doesn't have spaces in it. And the next time
    anyone puts a space in a filename on your system, shoot them. It will save
    you a lot of hassle.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Jul 14, 2006
    #3
  4. Simon Biber Guest

    wrote:
    > I have a program which reads in 3 filenames from the command line
    >
    > prog filename1 filename2 filename3
    >
    > However, it doesn't work when one of the filenames has spaces in it
    > (due to a directory name with a space in it) because that filename gets
    > split into 2.
    >
    > I tried
    >
    > prog "/blah/blah 2/filename1" "filename2" "filename3"
    >
    > and it still splits on the space. (It included the " chars too).


    That's strange; what you have given above works on most platforms.

    Strictly, the way in which program arguments are supplied is not defined
    by the C standard. The C standard does not require that there is such a
    thing as a command line, or that arguments are separated by spaces.

    <OT>
    As Rouben said, on Unix platforms it is up to the shell to parse the
    command line and separate out the individual arguments. Unix shells
    typically support double quotes, single quotes and also backspace
    escaping of spaces.

    On the Windows command line, double quotes are supported and will result
    in the correct escaping behaviour:

    C:\docs\prog\c>testargs "hello world" "foo"
    argc = 3
    argv[0] = testargs
    argv[1] = hello world
    argv[2] = foo

    But single quotes will not be recognised as anything special:

    C:\docs\prog\c>testargs 'hello world' 'foo'
    argc = 4
    argv[0] = testargs
    argv[1] = 'hello
    argv[2] = world'
    argv[3] = 'foo'

    </OT>

    > Is there a quick fix that would be possible here? I realise that it
    > could be fixed by manually detecting the " and creating the 3
    > filenames. However, if there was a way to write the command line so
    > that isn't necessary, that would be better/easier.


    You'll have to do whatever is necessary to make it work the way you want
    on your platform. Detecting the double quotes sounds like a possible option.

    --
    Simon.
     
    Simon Biber, Jul 14, 2006
    #4
  5. Al Balmer Guest

    On 14 Jul 2006 07:54:01 -0700, wrote:

    >I have a program which reads in 3 filenames from the command line
    >
    >prog filename1 filename2 filename3
    >
    >However, it doesn't work when one of the filenames has spaces in it
    >(due to a directory name with a space in it) because that filename gets
    >split into 2.
    >
    >I tried
    >
    >prog "/blah/blah 2/filename1" "filename2" "filename3"
    >
    >and it still splits on the space. (It included the " chars too).
    >
    >Is there a quick fix that would be possible here? I realise that it
    >could be fixed by manually detecting the " and creating the 3
    >filenames. However, if there was a way to write the command line so
    >that isn't necessary, that would be better/easier.


    Not only is this off-topic here, but it depends on your OS and what
    command shell you're using. Ask your question in a newsgroup dealing
    with your particular environment.

    --
    Al Balmer
    Sun City, AZ
     
    Al Balmer, Jul 14, 2006
    #5
  6. Guest

    Al Balmer wrote:

    > On 14 Jul 2006 07:54:01 -0700, wrote:
    >
    > >I have a program which reads in 3 filenames from the command line
    > >
    > >prog filename1 filename2 filename3
    > >
    > >However, it doesn't work when one of the filenames has spaces in it
    > >(due to a directory name with a space in it) because that filename gets
    > >split into 2.
    > >
    > >I tried
    > >
    > >prog "/blah/blah 2/filename1" "filename2" "filename3"
    > >
    > >and it still splits on the space. (It included the " chars too).
    > >
    > >Is there a quick fix that would be possible here? I realise that it
    > >could be fixed by manually detecting the " and creating the 3
    > >filenames. However, if there was a way to write the command line so
    > >that isn't necessary, that would be better/easier.

    >
    > Not only is this off-topic here, but it depends on your OS and what
    > command shell you're using. Ask your question in a newsgroup dealing
    > with your particular environment.



    Sorry.

    Anyway, I went with using " and manually parsing. The problem was that
    it needs to run on UNIX and windows (with the same .c file preferably).

    I was wondering if there was some c function that would take any
    possible OS and give a standard result.
     
    , Jul 14, 2006
    #6
  7. Skarmander Guest

    Richard Heathfield wrote:
    > said:
    >
    >> I have a program which reads in 3 filenames from the command line
    >>
    >> prog filename1 filename2 filename3
    >>
    >> However, it doesn't work when one of the filenames has spaces in it
    >> (due to a directory name with a space in it) because that filename gets
    >> split into 2.
    >>
    >> I tried
    >>
    >> prog "/blah/blah 2/filename1" "filename2" "filename3"
    >>
    >> and it still splits on the space. (It included the " chars too).
    >>
    >> Is there a quick fix that would be possible here?

    >
    > Yes. Rename the file so that it doesn't have spaces in it. And the next time
    > anyone puts a space in a filename on your system, shoot them. It will save
    > you a lot of hassle.
    >

    It will certainly save you more hassle than trying to convince every Unix
    programmer out there that spaces are not the tool of the devil, and users
    who want readable file names not criminal.

    S.
     
    Skarmander, Jul 14, 2006
    #7
  8. Richard Heathfield <> writes:
    > said:
    >
    >> I have a program which reads in 3 filenames from the command line
    >>
    >> prog filename1 filename2 filename3
    >>
    >> However, it doesn't work when one of the filenames has spaces in it
    >> (due to a directory name with a space in it) because that filename gets
    >> split into 2.
    >>
    >> I tried
    >>
    >> prog "/blah/blah 2/filename1" "filename2" "filename3"
    >>
    >> and it still splits on the space. (It included the " chars too).
    >>
    >> Is there a quick fix that would be possible here?

    >
    > Yes. Rename the file so that it doesn't have spaces in it. And the next time
    > anyone puts a space in a filename on your system, shoot them. It will save
    > you a lot of hassle.


    Unfortunately, that's not always possible. Certain operating systems
    have top-level directories whose names have spaces in them, making it
    difficult or impossible to give a space-free full pathname for many
    files.

    Nothing in C cares whether file names contains spaces or not. The
    filename argument to fopen() is just a string; if the OS allows spaces
    in file names, fopen() does too.

    As for command-line arguments, main() receives them as a sequence of
    strings; again, spaces aren't special. It's up to the calling
    environment to pass the correct strings.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jul 14, 2006
    #8
  9. writes:
    > Al Balmer wrote:
    >> On 14 Jul 2006 07:54:01 -0700, wrote:
    >>
    >> >I have a program which reads in 3 filenames from the command line
    >> >
    >> >prog filename1 filename2 filename3
    >> >
    >> >However, it doesn't work when one of the filenames has spaces in it
    >> >(due to a directory name with a space in it) because that filename gets
    >> >split into 2.
    >> >
    >> >I tried
    >> >
    >> >prog "/blah/blah 2/filename1" "filename2" "filename3"
    >> >
    >> >and it still splits on the space. (It included the " chars too).
    >> >
    >> >Is there a quick fix that would be possible here? I realise that it
    >> >could be fixed by manually detecting the " and creating the 3
    >> >filenames. However, if there was a way to write the command line so
    >> >that isn't necessary, that would be better/easier.

    >>
    >> Not only is this off-topic here, but it depends on your OS and what
    >> command shell you're using. Ask your question in a newsgroup dealing
    >> with your particular environment.

    >
    > Sorry.
    >
    > Anyway, I went with using " and manually parsing. The problem was that
    > it needs to run on UNIX and windows (with the same .c file preferably).
    >
    > I was wondering if there was some c function that would take any
    > possible OS and give a standard result.


    If your program expects a sequence of file names as command-line
    arguments, it should get them. argv[1] should point to a string
    containing the first file name, argv[2] to a string containing the
    second, and so forth, with argc telling you how many you have.
    Normally your program shouldn't care about spaces and quotation marks
    (unless needs to do its own special-purpose parsing, but that doesn't
    seem to be the case here).

    How these strings are passed into your program is up to the calling
    environment.

    <OT>
    On Unix-like systems, for example, assuming

    prog "/blah/blah 2/filename1" "filename2" "filename3"

    is what you type at a shell prompt, your program should see three
    arguments; the first one happens to contain a space. None of the
    arguments will contain a '"' character. I *think* it's the same for
    MS-DOS and Windows. (VMS is more complicated, but there are ways to
    do what you want.)
    </OT>

    Here's a simple program that shows the actual command-line arguments:

    #include <stdio.h>
    int main(int argc, char **argv)
    {
    int i;
    printf("argc = %d\n", argc);
    for (i = 1; i < argc; i ++) {
    printf("argv[%d] = \"%s\"\n", i, argv);
    }
    return 0;
    }

    If I invoke it as
    prog foo bar
    I get:
    argc = 3
    argv[1] = "foo"
    argv[2] = "bar"
    If I invoke it as
    prog "foo bar"
    I get:
    argc = 2
    argv[1] = "foo bar"

    If you're not able to get these same results, you'll need to ask in a
    newsgroup that's specific to whatever system you're using. If you're
    having problems on both Unix and Windows, I suggest posting separately
    to two different newsgroups. Be sure to specify your environment
    (such as which operating system you're using, what shell you're using,
    and the *exact* command line you enter; copy-and-paste it, don't
    re-type or paraphrase it).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jul 14, 2006
    #9
  10. On Fri, 14 Jul 2006 15:23:08 +0000, in comp.lang.c , Richard
    Heathfield <> wrote:

    > said:
    >
    >Yes. Rename the file so that it doesn't have spaces in it. And the next time
    >anyone puts a space in a filename on your system, shoot them. It will save
    >you a lot of hassle.


    I have to say, this is pretty silly advice. Do you have a problem with
    readable filenames?

    My pet hate is OSen that permit carriage returns and newlines in
    filenames. Seriously screws grep and awk, I can tell you.
    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
     
    Mark McIntyre, Jul 14, 2006
    #10
  11. Keith Thompson said:

    > Richard Heathfield <> writes:

    <snip>
    >>
    >> Rename the file so that it doesn't have spaces in it. And the next
    >> time anyone puts a space in a filename on your system, shoot them. It
    >> will save you a lot of hassle.

    >
    > Unfortunately, that's not always possible. Certain operating systems
    > have top-level directories whose names have spaces in them, making it
    > difficult or impossible to give a space-free full pathname for many
    > files.


    That's easy to solve. Simply remove those directories. :)

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Jul 14, 2006
    #11
  12. Mark McIntyre said:

    > On Fri, 14 Jul 2006 15:23:08 +0000, in comp.lang.c , Richard
    > Heathfield <> wrote:
    >
    >> said:
    >>
    >>Yes. Rename the file so that it doesn't have spaces in it. And the next
    >>time anyone puts a space in a filename on your system, shoot them. It will
    >>save you a lot of hassle.

    >
    > I have to say, this is pretty silly advice.


    I suppose you're right - it'd cost a fortune in ammunition.

    > Do you have a problem with readable filenames?


    No - I always use readable filenames, and none of them contains spaces.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Jul 14, 2006
    #12
  13. Al Balmer Guest

    On 14 Jul 2006 10:58:49 -0700, wrote:

    >
    >Al Balmer wrote:
    >
    >> On 14 Jul 2006 07:54:01 -0700, wrote:
    >>
    >> >I have a program which reads in 3 filenames from the command line
    >> >
    >> >prog filename1 filename2 filename3
    >> >
    >> >However, it doesn't work when one of the filenames has spaces in it
    >> >(due to a directory name with a space in it) because that filename gets
    >> >split into 2.
    >> >
    >> >I tried
    >> >
    >> >prog "/blah/blah 2/filename1" "filename2" "filename3"
    >> >
    >> >and it still splits on the space. (It included the " chars too).
    >> >
    >> >Is there a quick fix that would be possible here? I realise that it
    >> >could be fixed by manually detecting the " and creating the 3
    >> >filenames. However, if there was a way to write the command line so
    >> >that isn't necessary, that would be better/easier.

    >>
    >> Not only is this off-topic here, but it depends on your OS and what
    >> command shell you're using. Ask your question in a newsgroup dealing
    >> with your particular environment.

    >
    >
    >Sorry.
    >
    >Anyway, I went with using " and manually parsing. The problem was that
    >it needs to run on UNIX and windows (with the same .c file preferably).
    >
    >I was wondering if there was some c function that would take any
    >possible OS and give a standard result.


    It isn't the C program that's the problem. The program will do fine
    once you persuade the OS to pass you the strings properly. That's why
    I said it's OS and shell dependent.

    Keep in mind that your solution may break on a system (or shell) that,
    e.g., strips the quotes and gives you the entire string as you
    expected before.

    I think it's still a good idea to ask in a group dealing with your
    environment. Personally, I wouldn't feel comfortable without knowing
    why it behaves that way, and when it might decide not to ;-)

    --
    Al Balmer
    Sun City, AZ
     
    Al Balmer, Jul 14, 2006
    #13
  14. Richard Heathfield <> writes:
    > Keith Thompson said:
    >> Richard Heathfield <> writes:

    > <snip>
    >>>
    >>> Rename the file so that it doesn't have spaces in it. And the next
    >>> time anyone puts a space in a filename on your system, shoot them. It
    >>> will save you a lot of hassle.

    >>
    >> Unfortunately, that's not always possible. Certain operating systems
    >> have top-level directories whose names have spaces in them, making it
    >> difficult or impossible to give a space-free full pathname for many
    >> files.

    >
    > That's easy to solve. Simply remove those directories. :)


    Yeah, and install Windows XP Service Pack 3, available from
    redhat.com. :cool:}

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jul 15, 2006
    #14
  15. Richard Heathfield wrote:
    > > Is there a quick fix that would be possible here?

    >
    > Yes. Rename the file so that it doesn't have spaces in it. And the next time
    > anyone puts a space in a filename on your system, shoot them. It will save
    > you a lot of hassle.


    Fifteen years ago (more?), a friend was writing a short story on his
    PC using WordStar, and having trouble. I suggested he use a name
    with no space. That's not the trouble, he insisted, but sure enough,
    some combination of Saves and Opens kept destroying his latest edit.
    I don't remember the details, but it was obvious two different
    routines,
    both invoked from the WordStar environment, treated space differently.

    What is with all the absurd names anyway? I don't have Linux
    software for my digital camera so process it via Windoze on
    a dual-boot system. Not only are all the names absurd and absurdly
    long, but AFAIK there's no easy way to change them in Windoze
    except one at a time. I end up booting Linux just to rename my
    Windoze files!

    James
     
    James Dow Allen, Jul 15, 2006
    #15
  16. <> wrote in message
    news:...
    > I have a program which reads in 3 filenames from the command line
    >
    > prog filename1 filename2 filename3
    >
    > However, it doesn't work when one of the filenames has spaces in it
    > (due to a directory name with a space in it) because that filename gets
    > split into 2.
    >
    > I tried
    >
    > prog "/blah/blah 2/filename1" "filename2" "filename3"
    >
    > and it still splits on the space. (It included the " chars too).
    >
    > Is there a quick fix that would be possible here? I realise that it
    > could be fixed by manually detecting the " and creating the 3
    > filenames. However, if there was a way to write the command line so
    > that isn't necessary, that would be better/easier.
    >


    As others have pointed out, this is environment specific. I had to deal
    with the issue for DJGPP and OpenWatcom compilers. For DJGPP, it has an
    extension that forces quoted args to remain be passed as is
    (_crt0_startup_flags=_CRT0_FLAG_KEEP_QUOTES). Unfortunately, OpenWatcom had
    no such nicety. But, OW has an additional function, getcmd(), which allows
    you to access the original command line. Using the original command line,
    you can loop through and requote or reparse the argv arguments. You
    probably need to search for such an extension to C.


    Rod Pemberton
     
    Rod Pemberton, Jul 15, 2006
    #16
  17. On Fri, 14 Jul 2006 21:42:54 +0000, in comp.lang.c , Richard
    Heathfield <> wrote:

    >Mark McIntyre said:
    >
    >> Do you have a problem with readable filenames?

    >
    >No - I always use readable filenames, and none of them contains spaces.


    Youapparentlyhaveadifferentdefintionofreadabletomosthumanbeings

    ibelievethatwritingwithoutanyspaceswentoutoffashionaround500adwhentheromansstartedusingspaces

    dylslvtllthvwls?

    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
     
    Mark McIntyre, Jul 15, 2006
    #17
  18. Mark McIntyre said:

    > On Fri, 14 Jul 2006 21:42:54 +0000, in comp.lang.c , Richard
    > Heathfield <> wrote:
    >
    >>Mark McIntyre said:
    >>
    >>> Do you have a problem with readable filenames?

    >>
    >>No - I always use readable filenames, and none of them contains spaces.

    >
    > Youapparentlyhaveadifferentdefintionofreadabletomosthumanbeings


    You appear to be mistaking "filename" with "complete description in verbose
    English of the contents of the file". If, for example, I have a program to
    apply a Bayesian filtering algorithm to my mailbox contents, I am likely to
    call it something like "killspam.c". I would not be so daft as to call it
    "programtoapplyabayesianfilteringalgorithmtomymailboxcontents.c" - if I
    want an exacting description of the file's contents, the filename is not
    the place to keep it.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Jul 15, 2006
    #18
  19. Joe Wright Guest

    Mark McIntyre wrote:
    > On Fri, 14 Jul 2006 21:42:54 +0000, in comp.lang.c , Richard
    > Heathfield <> wrote:
    >
    >> Mark McIntyre said:
    >>
    >>> Do you have a problem with readable filenames?

    >> No - I always use readable filenames, and none of them contains spaces.

    >
    > Youapparentlyhaveadifferentdefintionofreadabletomosthumanbeings
    >
    > ibelievethatwritingwithoutanyspaceswentoutoffashionaround500adwhentheromansstartedusingspaces
    >
    > dylslvtllthvwls?
    >

    The-hyphen-is-acceptable-in-filenames. Readable?

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Jul 15, 2006
    #19
  20. Joe Wright posted:

    > Mark McIntyre wrote:
    >> On Fri, 14 Jul 2006 21:42:54 +0000, in comp.lang.c , Richard
    >> Heathfield <> wrote:
    >>
    >>> Mark McIntyre said:
    >>>
    >>>> Do you have a problem with readable filenames?
    >>> No - I always use readable filenames, and none of them contains
    >>> spaces.

    >>
    >> Youapparentlyhaveadifferentdefintionofreadabletomosthumanbeings
    >>
    >> ibelievethatwritingwithoutanyspaceswentoutoffashionaround500adwhentherom
    >> ansstartedusingspaces
    >>
    >> dylslvtllthvwls?
    >>

    > The-hyphen-is-acceptable-in-filenames. Readable?



    Not_to_mention_the_underscore


    --

    Frederick Gotham
     
    Frederick Gotham, Jul 15, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. d davis
    Replies:
    0
    Views:
    501
    d davis
    Apr 27, 2004
  2. qazmlp
    Replies:
    7
    Views:
    554
    Adrian
    Oct 31, 2004
  3. Anonieko

    HttpHandlers - Learn Them. Use Them.

    Anonieko, Jun 15, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    566
    tdavisjr
    Jun 16, 2006
  4. Dawn Minnis
    Replies:
    9
    Views:
    391
    Dave Thompson
    Feb 28, 2005
  5. Replies:
    1
    Views:
    133
    Ben Morrow
    Nov 10, 2006
Loading...

Share This Page