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

V

Vinu

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
 
V

Vinu

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
 
H

hari4063

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);
fgetc(stdin);
return 0;
}
 
V

Vinu

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

Krishanu Debnath

Vinu said:
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 ?

Post with proper context/quote is much appreciated.

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

Krishanu
 
V

vijay

Vinu said:
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 ?

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

Vinu

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
 
L

Lawrence Kirby

hai

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

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

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

{

return o;


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

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

return 0;
}
 
V

Vinu

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

Chris Torek

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

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

Keith Thompson

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

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

Vinu

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

Keith Thompson

Vinu said:
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?

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

That's right, you get undefined behavior.

[...]
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.

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

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.
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top