473,569 Members | 2,770 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

main(int argc, wchar_t *argv[]) on linux i386 (redhat)

Hi Folks,

I am writing a c++ program on redhat linux using main(int argc,
wchar_t *argv[]). $LANG on console is set to "en_US.UTF-8". g++
compiler version is 3.4.6.

Q1. what is the encoding of data that I get in argv[] ?

Q2. what is encoding of string constants defined in programs (for
example L"--count") ?

Q3. when I run the program as:

./a.out --count 1

Why does (wcscmp(argv[1], L"--count") == 0) always evaluate to
false?
What is the workaround. How do I make it evaluate to true?

Q4: when I do wstring

mystring = argv[1];

is mystring considered to be a UTF-8 string?
Q5: In case I use main(int argc, char** argv),

what is the encoding of characters in argv[] ?

Q6. What is the difference between main(int arc, wchar_t *argv[]) and
main(int argc, char *argv[]). Any document that describes this? My
compiler is gcc 3.4.6

Thanks

Mar 23 '07 #1
4 9762
in*****@interec .net wrote:
Hi Folks,

I am writing a c++ program on redhat linux using main(int argc,
wchar_t *argv[]). $LANG on console is set to "en_US.UTF-8". g++
compiler version is 3.4.6.
main(int argc,wchar_t *argv[]) is not standard. It may compile and link
but it's probably not doing what you think it does.
>
Q1. what is the encoding of data that I get in argv[] ?
The encoding of the command line. It will probably be an 8bit /
multibyte encoding, dot a wide character (wchar_t) encoding.
>
Q2. what is encoding of string constants defined in programs (for
example L"--count") ?
This is a g++ question but in short, it depends on the compiler and the
encoding of the source file.
>
Q3. when I run the program as:

./a.out --count 1

Why does (wcscmp(argv[1], L"--count") == 0) always evaluate to
false?
What is the workaround. How do I make it evaluate to true?
Don't use what_t
>
Q4: when I do wstring

mystring = argv[1];

is mystring considered to be a UTF-8 string?
try it and see.
>

Q5: In case I use main(int argc, char** argv),

what is the encoding of characters in argv[] ?
The encoding the caller put in there.
>
Q6. What is the difference between main(int arc, wchar_t *argv[]) and
main(int argc, char *argv[]). Any document that describes this? My
compiler is gcc 3.4.6
Check with the GCC docs, but I don't think main(int arc, wchar_t
*argv[]) is supported at all.
>
Thanks
Mar 23 '07 #2
Dnia Fri, 23 Mar 2007 14:54:52 -0700, interec napisa³(a):
I am writing a c++ program on redhat linux using
main(int argc, wchar_t *argv[]).
C++ Standard specifies only those two signatures for main:

int main();
int main(int argc, char** argv); //or char* argv[], whatever

Other signatures are allowed only in embedded environments.
$LANG on console is set to "en_US.UTF-8".
Console character encoding doesn't matter.
Q1. what is the encoding of data that I get in argv[] ?
Probably the one used for command line.
Q2. what is encoding of string constants defined in
programs (for example L"--count") ?
The encoding of string literal constants isn't precisely
defined and in most cases it is mapped from source character
set to machine code directly, without changes.
Q3. when I run the program as:

./a.out --count 1

Why does (wcscmp(argv[1], L"--count") == 0) always
evaluate to false?
Because L"--count" is composed from wide characters [stored
in more bytes than a sizeof(char) and of 'wchar_t' type].
It is encoded in most cases using Unicode UTF-16. So, unlike
your console, it is not UTF-8].
What is the workaround. How do I make it evaluate to true?
Use standard signature:

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

and interpret argv[1] as UTF-8. Characters in UTF-8 encoded
strings are plain one-byte 'char's but the international
characters are encoded as more-than-one-byte sequences.
If you program for Unix-like system, you may use iconv library
to transcode strings from/to multiple character sets.
Q4: when I do

wstring mystring = argv[1];

is mystring considered to be a UTF-8 string?
No. It is considered to be wide-character string.
UTF-8 is not a wide-character string, it's normal
one-byte-character string, but some characters are
encoded as multiple bytes.
Q5: In case I use main(int argc, char** argv),
what is the encoding of characters in argv[] ?
The same as in the command line.
Q6. What is the difference between

main(int arc, wchar_t *argv[])

and
main(int argc, char *argv[]).
The former is not standard.
Any document that describes this?
ANSI/ISO/IEC Standard 14882 - The C++ Programming Language

--
SasQ
Mar 24 '07 #3
Ok, here is a sample program. $LANG is set to "en_US.UTF-8" on my
console. When I invoke this program from command line as:

$ ./a.out "आ१२३ॠªà¥«à¥¬"

argument argv[1] gets set to an array of multibyte characters in UTF-8
format. Correct? Then as a test, I convert argv[1] to a wstring as
follows:

wstring sSql = make_wide(argv[1]);

and finally I print it out back to the console as:

wcout << sSql << endl;

The problem is that nothing is getting printed out on the console. I
tried using locale, but that didn't help either. Could someone please
explain whats going on and how I can print out wstring here. Full
sample code is given below. My compiler is g++ 3.4.6. OS is red hat
linux 4ES.

If I invoke the same program using english characters, everything
works as expected:

$ ./a.out "select"
select

The problem occurs only when I enter multi-byte characters as command
line args.

------------------------------------------------ SAMPLE PROGRAM
------------------------------------------------
#include <iostream>
#include <iomanip>
#include <locale>

using namespace std;

string make_narrow(con st wstring sArg);
wstring make_wide(const string sArg);

string make_narrow(con st wstring sArg)
{
std::string result;
std::locale loc;
for(unsigned int i= 0; i < sArg.size(); ++i)
{
result += std::use_facet< std::ctype<wcha r_t>
>(loc).narrow(s Arg[i], 0);
}
return result;
}

wstring make_wide(const string sArg)
{
std::wstring result;
std::locale loc;
for(unsigned int i= 0; i < sArg.size(); ++i)
{
result += std::use_facet< std::ctype<wcha r_t>
>(loc).widen(sA rg[i]);
}
return result;
}

/**
* main is supposed to take command line argument string argv[1] as
* input and print it out back on console in std::wstring form.
*
* $LANG is set to "en_US.UTF-8" on console.
*
* This function works with argv[1] contains only english characters.
* Function does not work with argv[1] contains a string containing
* multibyte characters.
*
*/
int main(int argc, char *argv[])
{
locale aLoc("en_US.UTF-8");
locale::global( aLoc);
wcout.imbue(aLo c);

wstring sSql = make_wide(argv[1]);

wcout << sSql << endl;
}

----------------------------------------------------------------------------------------------------------------

On Mar 23, 6:46 pm, SasQ <s...@go2.plwro te:
Dnia Fri, 23 Mar 2007 14:54:52 -0700, interec napisa³(a):
I am writing a c++ program on redhat linux using
main(int argc, wchar_t *argv[]).

C++ Standard specifies only those two signatures for main:

int main();
int main(int argc, char** argv); //or char* argv[], whatever

Other signatures are allowed only in embedded environments.
$LANG on console is set to "en_US.UTF-8".

Console character encoding doesn't matter.
Q1. what is the encoding of data that I get in argv[] ?

Probably the one used for command line.
Q2. what is encoding of string constants defined in
programs (for example L"--count") ?

The encoding of string literal constants isn't precisely
defined and in most cases it is mapped from source character
set to machine code directly, without changes.
Q3. when I run the program as:
./a.out --count 1
Why does (wcscmp(argv[1], L"--count") == 0) always
evaluate to false?

Because L"--count" is composed from wide characters [stored
in more bytes than a sizeof(char) and of 'wchar_t' type].
It is encoded in most cases using Unicode UTF-16. So, unlike
your console, it is not UTF-8].
What is the workaround. How do I make it evaluate to true?

Use standard signature:

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

and interpret argv[1] as UTF-8. Characters in UTF-8 encoded
strings are plain one-byte 'char's but the international
characters are encoded as more-than-one-byte sequences.
If you program for Unix-like system, you may use iconv library
to transcode strings from/to multiple character sets.
Q4: when I do
wstring mystring = argv[1];
is mystring considered to be a UTF-8 string?

No. It is considered to be wide-character string.
UTF-8 is not a wide-character string, it's normal
one-byte-character string, but some characters are
encoded as multiple bytes.
Q5: In case I use main(int argc, char** argv),
what is the encoding of characters in argv[] ?

The same as in the command line.
Q6. What is the difference between
main(int arc, wchar_t *argv[])
and
main(int argc, char *argv[]).

The former is not standard.
Any document that describes this?

ANSI/ISO/IEC Standard 14882 - The C++ Programming Language

--
SasQ

Mar 24 '07 #4
On Mar 23, 11:04 pm, inte...@interec .net wrote:
Ok, here is a sample program. $LANG is set to "en_US.UTF-8" on my
console. When I invoke this program from command line as:

$ ./a.out "आ१२३ॠªà¥«à¥¬"

argument argv[1] gets set to an array of multibyte characters in UTF-8
format. Correct?
The behaviour you're asking about is not defined by the C++ language.
You may wish to investigate, eg., comp.unix.progr ammer, or your
distribution's documentation about how it launches programs.
Then as a test, I convert argv[1] to a wstring as follows:

wstring sSql = make_wide(argv[1]);

and finally I print it out back to the console as:

wcout << sSql << endl;

The problem is that nothing is getting printed out on the console. I
tried using locale, but that didn't help either.
Have you tried examining the actual bytes being received in the argv
entries? Here's a short program (C++) that prints the character, hex
value, and decimal value of each character of each argv entry; try
running it with the above command line and seeing what it prints.

// Begin args.cpp
#include <iostream>
#include <iomanip>

void hexDump (const char *data, std::ostream &out) {
using std::endl;
using std::hex;
using std::dec;

for (int character = 0; data[character]; ++character) {
int asInt = data[character];

out << "Byte " << character << ": ";
out << hex << asInt;
out << " (" << dec << asInt << "): ";
out << data[character] << endl;
}
}

int main (int argc, char *argv[]) {
using std::cout;
using std::endl;

for (int argIndex = 0; argIndex < argc; ++argIndex) {
const char *argument = argv[argIndex];

cout << "Argument " << argIndex << ":" << endl;
hexDump (argument, cout);
}
}
// End of args.cpp

Then at least you will be aware of the encoded representation of the
command-line arguments. You may need to dike out out <<
data[character] part if the encoding is multibyte and any of the
individual bytes are interpreted as control characters; alternately,
<ot>pipe the output through less(1), which sanitizes its input before
displaying it</ot>.
#include <iostream>
#include <iomanip>
#include <locale>
#include <string// for std::wstring and std::string
>
using namespace std;
You should probably pick one of "using namespace std;" and using fully-
qualified names. Below you do both, whcih is somewhat confusing.
string make_narrow(con st wstring sArg);
wstring make_wide(const string sArg);

string make_narrow(con st wstring sArg)
{
std::string result;
std::locale loc;
// ...
Finally, please trim quoted text and reply inline, rather than quoting
the entire thread yet one more time and replying at the top. The
former is the accepted style for this newsgroup and the latter reads
"backwards" .

Mar 24 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
2183
by: Steven T. Hatton | last post by:
The short sample program listed below has some features that I find to be bad style. In particular, they fail to communicate the connection between names used in this program and the location in which they are declared and or defined. I'd like to know if there are any features of this code you believe represents bad programming technique. How...
9
2828
by: Vinu | last post by:
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<<std::endl; std::wcout<<L" Argument Count "<<argc-1<<std::endl;
4
4270
by: Vinu | last post by:
hi How can i accept unicode char in command line. The application on Solaris Sparc expects wide characters for its processing Can you suggest standard way of doing it on Sun Solaris Sparc? We are using gcc 3.4.2 . Thanks Vinu
14
39763
by: Hal Styli | last post by:
Is this a style thing? int main(int argc, char *argv ) or int main(int argc, char **argv ) i.e. *argv or **argv Why choose the latter?
13
7752
by: Sokar | last post by:
I have my main function set up as int main(int argv, char *argv) so taht i can read in a variable which is passed to the program on the command line. The problem is that main calls other functions and some of them need to start jump to the start of main. If my main setup was just int main() this would be no problem, i could just call...
1
10958
by: mudanoman | last post by:
Hello, I am new to c and need help with a current program I am working on. Currently, the program (code below) input is as follows: program apple.test.com 51112 What I need is (essentially three arguments with a ':' space between the hostname and port number, and a third argument):
7
4797
by: =?Utf-8?B?Vmlu?= | last post by:
Hi, I have a question. I created a simple executable program using Visual C++ from Visual Studio 6.0 This program is called from a script that passes in one argument. Now, my question is: 1. When I use int main(int argc, char* argv) declaration, the argv usually
3
4780
by: Bill Cunningham | last post by:
I have been having a little trouble with this page. http://www.physics.drexel.edu/courses/Comp_Phys/General/C_basics/#command-line I wanted to create a command called div that takes two parameters a numerator and denomenator that works as such: div 3 15 and the return should be 5. The coding on this page is pretty poor IMO. Atleast I'm...
2
11854
by: significantBit | last post by:
n00b here. Just started learning C a couple of days ago. I'm using xcode alternatively with emacs. My question is in regards to the main function. Everytime I create a project (standard command utility) with xcode, my main function starts out looking like this: int main(int argc, const char * argv)
0
7694
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7921
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7666
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.