6
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)

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Cenoc
  • 11,172
  • 21
  • 58
  • 92
  • 1
    What is `_tmain`? In what environment are you working? – Tomer Vromen Jul 09 '10 at 18:06
  • See the answer in this question http://stackoverflow.com/questions/895827/what-is-the-difference-between-tmain-and-main-in-c – Dave Bacher Jul 09 '10 at 18:08
  • I don't think it's a duplicate. It's phrased differently, and you are not the first developer I've seen stumbling on exactly these symptoms. So this formulation at least should stay IMHO. – EFraim Jul 09 '10 at 18:26

3 Answers3

8

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
  • 12,811
  • 4
  • 46
  • 62
2

Your argument string is coming in as UNICODE.

See this question

Community
  • 1
  • 1
msergeant
  • 4,771
  • 3
  • 25
  • 26
0

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
  • 48,066
  • 12
  • 84
  • 109