Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > command line arguements with spaces in them

Reply
Thread Tools

command line arguements with spaces in them

 
 
raphfrk@netscape.net
Guest
Posts: n/a
 
      07-14-2006
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.

 
Reply With Quote
 
 
 
 
Rouben Rostamian
Guest
Posts: n/a
 
      07-14-2006
In article <(E-Mail Removed). com>,
<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      07-14-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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)
 
Reply With Quote
 
Simon Biber
Guest
Posts: n/a
 
      07-14-2006
(E-Mail Removed) 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.
 
Reply With Quote
 
Al Balmer
Guest
Posts: n/a
 
      07-14-2006
On 14 Jul 2006 07:54:01 -0700, (E-Mail Removed) 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
 
Reply With Quote
 
raphfrk@netscape.net
Guest
Posts: n/a
 
      07-14-2006

Al Balmer wrote:

> On 14 Jul 2006 07:54:01 -0700, (E-Mail Removed) 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.

 
Reply With Quote
 
Skarmander
Guest
Posts: n/a
 
      07-14-2006
Richard Heathfield wrote:
> (E-Mail Removed) 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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      07-14-2006
Richard Heathfield <(E-Mail Removed)> writes:
> (E-Mail Removed) 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) (E-Mail Removed) <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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      07-14-2006
(E-Mail Removed) writes:
> Al Balmer wrote:
>> On 14 Jul 2006 07:54:01 -0700, (E-Mail Removed) 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[i]);
}
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) (E-Mail Removed) <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.
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      07-14-2006
On Fri, 14 Jul 2006 15:23:08 +0000, in comp.lang.c , Richard
Heathfield <(E-Mail Removed)> wrote:

>(E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How to trim a String trailing spaces, but not leading spaces? Roedy Green Java 3 09-14-2008 02:10 AM
Re: How to trim a String trailing spaces, but not leading spaces? John B. Matthews Java 4 09-12-2008 05:28 AM
perl command line arguements mrinalini.sukumar@gmail.com Perl Misc 1 11-10-2006 02:45 AM
accessing and storing character arguements from command line Dawn Minnis C Programming 9 02-28-2005 07:01 AM
sending command line arguements to HTML Tidy d davis Perl 0 04-27-2004 02:23 PM



Advertisments