command line arguements with spaces in them

R

raphfrk

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.
 
R

Rouben Rostamian

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.
 
R

Richard Heathfield

(e-mail address 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.
 
S

Simon Biber

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'

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.
 
A

Al Balmer

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.
 
R

raphfrk

Al said:
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.
 
S

Skarmander

Richard said:
(e-mail address 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.
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.
 
K

Keith Thompson

Richard Heathfield said:
(e-mail address 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.

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.
 
K

Keith Thompson

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).
 
M

Mark McIntyre

(e-mail address 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
 
R

Richard Heathfield

Keith Thompson said:
Richard Heathfield <[email protected]> writes:

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. :)
 
R

Richard Heathfield

Mark McIntyre said:
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.
 
A

Al Balmer

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 ;-)
 
K

Keith Thompson

Richard Heathfield said:
Keith Thompson said:

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

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

James Dow Allen

Richard 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.

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
 
R

Rod Pemberton

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
 
M

Mark McIntyre

Mark McIntyre said:


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
 
R

Richard Heathfield

Mark McIntyre said:
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.
 
J

Joe Wright

Mark said:
Youapparentlyhaveadifferentdefintionofreadabletomosthumanbeings

ibelievethatwritingwithoutanyspaceswentoutoffashionaround500adwhentheromansstartedusingspaces

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top