views:

119

answers:

3
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 dont 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)

+6  A: 

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)

EFraim
Would you happen to know how to convert to char?
Cenoc
@Cenoc: http://stackoverflow.com/questions/159442/what-is-the-simplest-way-to-convert-char-to-from-tchar-in-c-cms
Martin York
Don't convert to `char` at all.
Philipp
+2  A: 

Your argument string is coming in as UNICODE.

See this question

msergeant
A: 

Don't use the char data type on Windows, it breaks Unicode support. Use the "wide" functions instead. In C++, avoid C's stdio, use file streams instead:

#include <cstdlib>
#include <string>
#include <fstream>
int wmain(int argc, wchar_t** argv) {
  if (argc <= 2) return EXIT_FAILURE;
  const std::wstring arg1 = argv[1];
  const std::wstring arg2 = argv[2];
  std::ifstream file1(arg1.c_str());
  std::ofstream file2(arg2.c_str());
}
Philipp