Re: [Dev-C++] arguments to a program
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Robert A. <ral...@gm...> - 2005-11-27 05:19:14
|
You have two options... first you need to understand that the OS is passing
the command like arguments to the program and that the OS determines how th=
e
arguments are separated.
So the easily way to handle it would be to change how the program is run
which would tell the OS that the first argument contains spaces.
./a.out "file name with spaces"
./a.out 'file name with spaces'
the other way to do it would be to assume that there is only one argument t=
o
your application (or that the last argument is the filename) and then build
up your filename and include spaces between arguments provided by the OS,
but you will have problems if there are two adjacent spaces with this
method.
./a,out file name with spaces
....
int main(int argc , char * argv[] )
{
char * filename =3D NULL;
unsigned int length =3D 0 ;
unsigned int marker =3D 0 ;
if ( argc <=3D 1 ) then exit(1) ;
for ( int i =3D 1 ; i < argc ; ++i )
length +=3D strlen(argv[i]) + 1 ;
filename =3D malloc(length);
for ( int i =3D 1 ; i < argc ; ++i )
{
for ( int j =3D 0 ; j < strlen(argv[i]) ; ++j )
filename[marker++] =3D argv[i][j] ;
filename[marker++] =3D ' ' ; /* inserting spaces between shell argumen=
ts
*/
}
/* backup and insert end of string marker */
filename[marker - 1] =3D '\0' ;
.....
......
On 11/26/05, Noorez Kassam <lor...@ho...> wrote:
>
> For a program i'm making, i need to pass a file name, but if it has a
> space,
> it seems to pass as two arguments, how do get aroung this problem?
>
> _________________________________________________________________
> Powerful Parental Controls Let your child discover the best the Internet
> has
> to offer.
>
> http://join.msn.com/?pgmarket=3Den-ca&page=3Dbyoa/prem&xAPID=3D1994&DI=3D=
1034&SU=3Dhttp://hotmail.com/enca&HL=3DMarket_MSNIS_Taglines
> Start enjoying all the benefits of MSN(r) Premium right now and get the
> first two months FREE*.
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc. Do you grep through log
> files
> for problems? Stop! Download the new AJAX search engine that makes
> searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
> http://ads.osdn.com/?ad_id=3D7637&alloc_id=3D16865&op=3Dclick
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|