Connecting Tech Pros Worldwide Help | Site Map

how i can print the values of argv[] under unix environment.Plz help me

Vinu
Guest
 
Posts: n/a
#1: Nov 14 '05
hai

i have a problem i con't able to print the values of argv[]

int main(int argc, wchar_t* argv[])
{

return o;

}

how i can print the values of argv[] under unix environment.Plz help me

Vinu
Guest
 
Posts: n/a
#2: Nov 14 '05

re: how i can print the values of argv[] under unix environment.Plz help me


Hai

when i compile the cpp file(cmdargs.cpp) i attached the output below
the program

int main(int argc, wchar_t* argv[])
{

std::wcout<<L"Name of the Program is
"<<*argv[0]<<std::endl;
std::wcout<<L" Argument Count "<<argc-1<<std::endl;
for(int args=1;args<argc;args++)
{
std::wcout<<L"Argument "<<args<<" is "<<
*(argv[args])<<std::endl;
}
return 0;
}


output :
~~~~~~~~~~~~~~
Name of the Program is <----------------only this much...........

-->

-->

-->

-->



#include<iostream>
#include <stdlib.h>
int main(int argc, wchar_t* argv[])
{
std::cout<<"Name of the Program is
"<<*argv[0]<<std::endl;
std::cout<<" Argument Count "<<argc-1<<std::endl;
for(int args=1;args<argc;args++)
{
std::cout<<"Argument "<<args<<" is "<<
*(argv[args])<<std::endl;
}
return 0;
}


cmdargs asd fgh jkl

output :
~~~~~~~~~~~~~~

Name of the Program is 1668113505
Argument Count 3
Argument 1 is 1634952192
Argument 2 is 1718052864
Argument 3 is 1785424896

-->

-->

-->

-->
here i got some nos but i want the strings how i can print the
values..plz help me
Thanks in advance
Vinu

hari4063
Guest
 
Posts: n/a
#3: Nov 14 '05

re: how i can print the values of argv[] under unix environment.Plz help me


If you use printf, You must specifie "l" flag:

#include <stdio.h>

int main(int argc, wchar_t** argv) {
int i;
for (i=0; i<argc; i++)
printf("%sl\n", argv[i]);
fgetc(stdin);
return 0;
}

Vinu
Guest
 
Posts: n/a
#4: Nov 14 '05

re: how i can print the values of argv[] under unix environment.Plz help me


why it is not giving the output

in 2'nd case it i giving the nos

i want to use cout...

can u help me out in this matter ?

pete
Guest
 
Posts: n/a
#5: Nov 14 '05

re: how i can print the values of argv[] under unix environment.Plz help me


Vinu wrote:
[color=blue]
> #include<iostream>[/color]

news:comp.lang.c++

--
pete
Krishanu Debnath
Guest
 
Posts: n/a
#6: Nov 14 '05

re: how i can print the values of argv[] under unix environment.Plz help me


Vinu wrote:[color=blue]
> why it is not giving the output
>
> in 2'nd case it i giving the nos
>
> i want to use cout...
>
> can u help me out in this matter ?
>[/color]

Post with proper context/quote is much appreciated.

If you want to use c++ , try comp.lang.c++(.moderated)?

Krishanu
vijay
Guest
 
Posts: n/a
#7: Nov 14 '05

re: how i can print the values of argv[] under unix environment.Plz help me



Vinu wrote:[color=blue]
> why it is not giving the output
>
> in 2'nd case it i giving the nos
>
> i want to use cout...
>
> can u help me out in this matter ?[/color]

comp.lang.c++ should have been the place to ask this question. Anyways,
use

for(int args=1;args<argc;args++)
{
cout<<"Argument "<<args<<" is "<<argv[args]<<endl;
}

You need a pointer to a string and argv[args] is a pointer to the
args'th argument.

regards,
vijay.

Vinu
Guest
 
Posts: n/a
#8: Nov 14 '05

re: how i can print the values of argv[] under unix environment.Plz help me


Hello vijay


int main(int argc, wchar_t* argv[])
{


std::cout<<"Name of the Program is
"<<*argv[0]<<std::endl;
std::cout<<" Argument Count "<<argc-1<<std::endl;
for(int args=1;args<argc;args++)
{
std::cout<<"Argument "<<args<<" is "<<
*(argv[args])<<std::endl;
}
return 0;
}

output :
~~~~~~~~

-->cmdargs asd fgh jkl
Name of the Program is 0xffbefd18
Argument Count 3
Argument 1 is 0xffbefd20
Argument 2 is 0xffbefd24
Argument 3 is 0xffbefd28

i changed my prog see one thing *argv[args] is also gives a pointer
wchar_t* argv[]

Vinu

Lawrence Kirby
Guest
 
Posts: n/a
#9: Nov 14 '05

re: how i can print the values of argv[] under unix environment.Plz help me


On Wed, 04 May 2005 04:52:00 -0700, Vinu wrote:
[color=blue]
> hai
>
> i have a problem i con't able to print the values of argv[]
>
> int main(int argc, wchar_t* argv[])[/color]

In standard C the type of argv should be char ** which can be written in
a parameter list as char *[] (i.e. char *argv[])

[color=blue]
> {
>
> return o;
>
>
> }
> how i can print the values of argv[] under unix environment.Plz help me[/color]

Here is a simple example:

#include <stdio.h>

int main(int argc, char *argv[]) {
int i;

for (i=0; i<argc; i++)
printf("%s\n", argv[i]);

return 0;
}

Vinu
Guest
 
Posts: n/a
#10: Nov 14 '05

re: how i can print the values of argv[] under unix environment.Plz help me


here it is not a wide char i want a unicode support cmd argumentts ie
wchar_t* argv[]
thanks

Chris Torek
Guest
 
Posts: n/a
#11: Nov 14 '05

re: how i can print the values of argv[] under unix environment.Plz help me


In article <1115271234.512896.198320@o13g2000cwo.googlegroups .com>
Vinu <vinuwarrier@yahoo.com> wrote:[color=blue]
>here it is not a wide char i want a unicode support cmd argumentts ie
>wchar_t* argv[]
>thanks[/color]

Your question (from the subject line) was about printing argv.
Presumably this means "the argv passed to main upon program startup".
(Your sample code was then in C++ rather than C, suggesting that
either you are not sure which language you are using, or that you
posted to the wrong newsgroup, or perhaps even both. Note that
when using "google groups", you are using Google's access portal
to the USENET newsgroup system. For the most part, USENET has
absolutely nothing to do with Google.)

With one exception, an implementation that passes "wchar_t **"
instead of "char **" as the second argument to the initial call to
main() is *not* a strictly conforming ANSI/ISO C implementation.
The exception occurs if that implementation makes wchar_t synonynous
with plain char, so that "wchar_t **" and "char **" are actually
the same type.

In any case, it is pretty silly to attempt to use wchar_t for argv.
(Only Microsoft would do something that dumb... :-) ) Use UTF-8.
It Just Works.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Keith Thompson
Guest
 
Posts: n/a
#12: Nov 14 '05

re: how i can print the values of argv[] under unix environment.Plz help me


"Vinu" <vinuwarrier@yahoo.com> writes:[color=blue]
> here it is not a wide char i want a unicode support cmd argumentts ie
> wchar_t* argv[]
> thanks[/color]

It's difficult to tell what you're talking about if you don't provide
any context.

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

The standard declaration for main is "int main(void)" or
"int main(int argc, char **argv)". An implementation may allow
other declarations, but anything other than the above is non-portable.

If you want to pass Unicode strings into your main program, you can
either use a method other than argc and argv or encode the Unicode
strings so they're representable as arrays of char <OT>UTF-8,
perhaps?</OT>.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <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.
Vinu
Guest
 
Posts: n/a
#13: Nov 14 '05

re: how i can print the values of argv[] under unix environment.Plz help me


Thanks for your suggestion and advice. I will try to redefine the
problem statement.

The application on Solaris Sparc expects wide characters for its
processing. One of the way of passing the wide characters will be pass
it through the 'int main(int argc, wchar_t* argv[])'. Is there any
alternative way of doing it?

While using the former way and passing the command line arguments to
the main function, we got bus error core dump while trying to access
the command line arguments as wide character strings.

Trying to print passed arguments to main usin wcout will print some
unsigned number instead of unicode string.

Are we doing any thing differently?

We have also tried to change main to wmain but it gives 'undefined
symbol main' 'ld: fatal: Symbol referencing errors. No output written
to <application name>' during linking.

Can you suggest standard way of doing it on Sun Solaris Sparc? We are
using gcc 3.4.2 .

Keith Thompson
Guest
 
Posts: n/a
#14: Nov 14 '05

re: how i can print the values of argv[] under unix environment.Plz help me


"Vinu" <vinuwarrier@yahoo.com> writes:[color=blue]
> Thanks for your suggestion and advice. I will try to redefine the
> problem statement.
>
> The application on Solaris Sparc expects wide characters for its
> processing. One of the way of passing the wide characters will be pass
> it through the 'int main(int argc, wchar_t* argv[])'. Is there any
> alternative way of doing it?[/color]

What makes you think that you can declare main like that and have it
work? C's main() function takes two arguments, int argc and
char **argv (or, equivalently, char *argv[]).

If you declare it to take different types of arguments, it's quite
possible that the compiler won't complain, but when environment
invokes your program it will still attempt to pass it an int and a
char**. In effect, you're lying to the compiler. What happens next?
[color=blue]
> While using the former way and passing the command line arguments to
> the main function, we got bus error core dump while trying to access
> the command line arguments as wide character strings.[/color]

That's right, you get undefined behavior.

[...]
[color=blue]
> We have also tried to change main to wmain but it gives 'undefined
> symbol main' 'ld: fatal: Symbol referencing errors. No output written
> to <application name>' during linking.[/color]

That's right, because "wmain" is not a standard identifier in C.
[color=blue]
> Can you suggest standard way of doing it on Sun Solaris Sparc? We are
> using gcc 3.4.2 .[/color]

There may be no way to do what exactly what you're trying to do. A
compiler is allowed to support declarations for main() other than the
standard ones, but as far as I know gcc does not.

If you want to get Unicode strings into your program, you'll just have
to find a different way to do it. The most obvious is to use a UTF-8
encoding.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <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.
Closed Thread