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[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)
kst- <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.