开发者

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 wh开发者_运维技巧y!

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

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


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)


Your argument string is coming in as UNICODE.

See this question


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());
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜