Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Main only receiving first letters of arguments

int _tmain(int argc, char** argv)  
    {  
      FILE* file1=fopen(argv[1],"r");  
      FILE* file2=fopen(argv[2],"w");  
    }

It seems as if only the first letter of the arguments is received... I don't get why!

std::cout<<"Opening "<<strlen(argv[1])<<" and writing to "<<strlen(argv[2])<<std::endl;

outputs 1 and 1 no matter what. (in MSVC 2010)

like image 264
Cenoc Avatar asked Jul 09 '10 18:07

Cenoc


People also ask

How are arguments passed to main?

To pass command line arguments, we typically define main() with two arguments : first argument is the number of command line arguments and second is list of command-line arguments. The value of argc should be non negative. argv(ARGument Vector) is array of character pointers listing all the arguments.

Is argv 1 The first argument?

argv[1] indicates the first argument passed to the program, argv[2] the second argument, and so on.

What is the first argument in command line?

argv[1] is the first command-line argument. The last argument from the command line is argv[argc - 1] , and argv[argc] is always NULL.

Can main function have arguments in C?

Yes, we can give arguments in the main() function. Command line arguments in C are specified after the name of the program in the system's command line, and these argument values are passed on to your program during program execution. The argc and argv are the two arguments that can pass to main function.


1 Answers

It's not char it's wchar_t when you are compiling with UNICODE set.

It is compiled as wmain. Linker just does not notice that there is a different signature, because it's "export C" function and it's name does not contain its argument types.

So it should be int _tmain(int argc, TCHAR** argv)

Converting to char is tricky and not always correct - Win32 provided function will only translate the current ANSI codepage correctly.

If you want to use UTF-8 in your application internals then you have to look for the converter elsewhere (such as in Boost)

like image 199
EFraim Avatar answered Oct 04 '22 20:10

EFraim