2

Possible Duplicate:
What is the difference between _tmain() and main() in C++?

OK, I have not clue of what is the problem here. I am trying to read some file names by using the argv in C++ but for some reason only the first character of the file name gets passed. I show you some part of the code.

int _tmain(int argc, char * argv[])
{
    int i;
    char *s1 = argv[1]; 
    printf("%s\n", s1);

    for( i=1; i<argc; i++ )
    printf("%s\n", argv[i]);
    return 0;
}

//The argv[1] I am passing is a file name like "TestFile.txt"
//The print out that I am getting is:
//T
//T

What is the problem here? Any help would be very much appreciate. Thanks.

Community
  • 1
  • 1
Bizwoo
  • 341
  • 1
  • 3
  • 6
  • 1
    Why is this tagged `c++`? It should be `c`. – sbi Nov 19 '10 at 10:56
  • 1
    @sbi: "I am trying to read some file names by using the argv **in C++** ". It should be tagged C++. Or no language, if you can't bear to see the C++ tag on code that uses `printf` ;-). – Steve Jessop Nov 19 '10 at 11:06
  • It should be `int _tmain(int argc, TCHAR *argv[])` – jwueller Nov 19 '10 at 11:08
  • @sbi: and how much progress are you making in your effort to get it removed from C++? Anyway, I can't bear to see the "C" tag on a question that is explicitly about C++, albeit not C++ written to your preferred style guide ;-p – Steve Jessop Nov 19 '10 at 11:38
  • @Steve: You have been overzealous. This question wasn't referring to C++ when you changed the tags. `:)` – sbi Nov 19 '10 at 11:45
  • @sbi: "I am trying to read some file names by using the argv in **C++** ". Obviously you could edit the question, to make it about C instead of C++, and then change the tags to match. And then someone else could come along and edit the question and the tags and the code snippet, because they decided they'd prefer the questioner to use Java instead of C *or* C++. I just don't think we should change the question to match what language we think the questioner *should* use. If the questioner claims to be compiling that code as C++, I currently have no reason to disbelieve that. – Steve Jessop Nov 19 '10 at 11:48
  • @sbi: of course we could tag it as both, since the answer in either is that the signature of `_tmain` is wrong. – Steve Jessop Nov 19 '10 at 11:50
  • @Steve: I agree, I have been wrong in doing so. Sorry. I apologize. Should we re-tag this now, or let it die silently? (I'm not sure whether this is an exact duplicate, so I wouldn't want to vote on this.) – sbi Nov 19 '10 at 16:38
  • @sbi: the questioner has their answer, so the tags only matter for future searching. On that basis it's probably best to tag it as both, although initially tagging a question as both could have drawn comments along the lines of "make your mind up, which is it". Hmm. – Steve Jessop Nov 19 '10 at 16:41

4 Answers4

1

My guess is that your environment uses wide character set. So you may try if wprintf or wcout works.

Chubsdad
  • 24,777
  • 4
  • 73
  • 129
  • @Let_Me_Be: exactly. Using `char*` with `_tmain` is the questioner's error. I'm slightly surprised the compiler doesn't complain. – Steve Jessop Nov 19 '10 at 11:04
0

your program works perfectly fine and here is the output.

$ ./a.out textfile.txt
textfile.txt
textfile.txt

Could you please check if you are using %s or %c as the format specifier?

jwueller
  • 30,582
  • 4
  • 66
  • 70
  • Did you compiled under Linux? _tmain is a Microsoft extension, so the OP is almost certainly compiling under Windows system. – Simone Nov 19 '10 at 10:55
0

C array indices start at zero so your initialization of s1 should read:

char* s1 = argv[0];
Yngve Hammersland
  • 1,634
  • 2
  • 14
  • 28
0

If you want an _tmain, you need to make a TCHAR* argv.

bmargulies
  • 97,814
  • 39
  • 186
  • 310