Simple (i think) question

This is a discussion on Simple (i think) question within the C Programming forums, part of the General Programming Boards category; Hi all, I'm writing C++ (which I hope to later compile with a Unix C compiler) and just want to ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    1

    Simple (i think) question

    Hi all,

    I'm writing C++ (which I hope to later compile with a Unix C compiler) and just want to compare command line arguments:

    Code:
    #include "stdafx.h"
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	char* outputdirectory;
    	char* searchdirectory;
    
    	for (int i=0;i<argc;i++)
    	{
    		if (strcmp(argv[i],"-o"))
    			outputdirectory = argv[i+1];
    		else if (strcmp(argv[i],"-d"))
    			searchdirectory = argv[i+1];
    
    
    	}
    }
    But I get this compiler error:

    Error 1 error C2664: 'strcmp' : cannot convert parameter 1 from '_TCHAR *' to 'const char *' 20
    Error 2 error C2440: '=' : cannot convert from '_TCHAR *' to 'char *' 21

    Does anyone have a thought? I'm guessing it's something simple I'm overlooking, or that I need to do some character conversion?

    Thanks...

    -Ben

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It's all TCHAR or all char. Choose one. (And if you choose TCHAR, you'll have to use the TCHAR version of strcmp, whose name escapes me at the moment. And the TCHAR version of string literals instead of "just things in quotes.")

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,593
    The best solution would be to make those char* into TCHAR* (or make all TCHAR* into char*).
    The actual problem stems from that TCHAR can either be a typedef for char or wchar_t, depending on project settings. Right now, it's set to unicode (it's default), so TCHAR is actually wchar_t, and wchar_t is not char.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Watch your _tmain() too if you will be looking to convert to UNIX later.

    My opinion is that _tmain and _TCHAR add more complexity and obfuscation than benefit.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    The short answer is, overwrite the Visual Studio's default main header and replace with:
    Code:
    int main(int argc, char *argv[])

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,593
    Better yet:
    Code:
    int main(int argc, char* argv[])
    Consistency!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 08:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 05:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21