473,387 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 9724
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(const wstring sArg);
wstring make_wide(const string sArg);

string make_narrow(const wstring sArg)
{
std::string result;
std::locale loc;
for(unsigned int i= 0; i < sArg.size(); ++i)
{
result += std::use_facet<std::ctype<wchar_t>
>(loc).narrow(sArg[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<wchar_t>
>(loc).widen(sArg[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(aLoc);

wstring sSql = make_wide(argv[1]);

wcout << sSql << endl;
}

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

On Mar 23, 6:46 pm, SasQ <s...@go2.plwrote:
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.programmer, 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(const wstring sArg);
wstring make_wide(const string sArg);

string make_narrow(const 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
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...
9
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;...
4
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...
14
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
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...
1
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...
7
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: ...
3
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.