Thread: Using strcpy from const char*

  1. #1
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209

    Using strcpy from const char*

    Hello there,

    I must admit, I'm TERRIBLE with filetypes. I remember the earlier days, where if I had a char to char* error or something similar, I'd just throw * or & operators in and hope something would work. Well, I'm not much better now.

    My problem is this : I'm using #define to define a value of text ( a filename ), and I want the program to check for this. If it ISN'T declared, it'll ask the user for input. Code goes like this :

    Code:
    //////// PRINT INPUT TO FILE
    void Encryptor::printinputfile()
    {
         #ifdef OUTFILENAME
         strcpy(filename.c_str(), OUTFILENAME);  // Where filename is a string
         
         #else
         cout << "\nFilename >> ";
         cin >> filename;
         cout << endl;
         #endif
         
         fout.open(filename.c_str(), ios::binary);
         fout << input;
         fout.close();
    }

    I'm getting "invalid conversion from const char* to char*" on the line that calls strcpy.

    So, is there a better / more intelligent way to do this ? Also, how would I solve this problem above ?

    Thanks very much,
    Quentin

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > strcpy(filename.c_str(), OUTFILENAME)
    c_str() is the read-only char* version of std::string.
    You only need to call this when you want the char* version to pass to some older style functions (like your open call).

    If you really want to assign to a std::string, you can do this
    filename = OUTFILENAME;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    EDIT: Salem answered it
    Last edited by Mario F.; 08-16-2006 at 05:48 AM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Mario, I've tried that, but because filename is a string (not char or array) it gives me "no matching function for call to strcpy(...)"
    Thanks though

    Salem, thanks, that works. However, is there a better way to do this than using a #define ?

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    You could perhaps test the value of filename. Being an empty string should be enough.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You should write your function to work either way without the use of the #define. Perhaps have it take a filename as a string parameter and default it to an empty string(). If the filename is empty, ask the user. You can then control the use of the default filename higher up in the code.

    If you want your option to be controlled by a preprocessor symbol, then just make a symbol that is either defined or not defined, and let that choose between passing a string to the printinputfile function or not. However, you could just use code to do that, perhaps by getting the option from the command line or somewhere else.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM