Thread: Adding character arrays to strings.

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    52

    Adding character arrays to strings.

    Hi, I'm writing a simple program to encrypt files ...and I want it to give it's output as enc_filename, or dec_filename when encrypting or decrypting, respectively. I get an error message:
    scrypt2.cpp:90: error: invalid operands of types ‘const char [5]’ and ‘char*’ to binary ‘operator+’

    for the line of code:
    Code:
    std::ofstream out("dec_" + argv[1]);
    any idea how I can get this to work?
    thanks.

    edit:: i also get that error for another line, where the only difference is that it has "enc_" instead of "dec_".....

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    If you don't mind std::string's:

    Code:
    std::ofstream out((std::string("dec_") += argv[1]).c_str());

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The hard way is to use strcat.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    std::ofstream out("dec_" + string(argv[1]));
    Will that work?
    Last edited by dwks; 02-04-2006 at 03:33 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    No, because the result will be an instance of string and the ofstream constructor takes a const char *. To fix it, do what Tonto did and call c_str() on the result of the +.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    52
    tonto's method works. but i have no clue what it does.... could you explain tonto?

    daved's would probably work, didn't try.
    dwk's doesn't work..

    hmm.
    also, a new problem has arisen:

    when it encrypts something it now appends enc_ to the output filename.
    when i tell it to decrypt that file, it appends dec - meaning it is now dec_enc_(filename).
    i don't want it to do that........... although i know why it does.

    any ideas on how to check if that's there and remove it from the..... umm... argv[2]?

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Instead of prepending dec_/enc_, why not store the file extension inside the file and encrypt it along with the rest of the data. Then, when you encrypt something, save it with a new extension (eg, *.enc) and when you decrypt it, you just read the extension and save it again with its original extension.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Use &argv[2][4].
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    52
    hmm jawib's idea has some merit.... and dwks, your idea might not work for everything - for example, picture files like pic_03.jpg or whatever.....
    i can throw the extension in at the beginning of the file.

    and does anybody know how tonto's idea works / what it is?

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Here's a breakdown:
    Code:
    std::ofstream out((std::string("dec_") += argv[1]).c_str());
    ->
    Code:
    std::string filename("dec_");
    filename += argv[1];
    std::ofstream out(filename.c_str());
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Dec 2005
    Posts
    52
    actually jawib's idea is kinda hard...
    hmm...........
    dwks, can i say :
    if (argv[2][0] == "enc_") or would i have to do like ((argv[2][0] == 'e') && (argv[2][1] == 'n').... etc

    hmm
    double hmm.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    strcmp()

    [edit]
    Code:
    #include <cstring>
    
    char astring[100];
    
    if(strcmp(astring, "hello") == 0) {
        // astring is "hello"
    }
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Or you could use strncpy:
    Code:
    #include <cstring>
    
    char s[100] = "enc_file";
    
    cout << s << endl;
    strncpy(s, "dec", 3);
    cout << s << endl;
    
    /* output:
    enc_file
    dec_file
    */
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by JaWiB
    Instead of prepending dec_/enc_, why not store the file extension inside the file and encrypt it along with the rest of the data. Then, when you encrypt something, save it with a new extension (eg, *.enc) and when you decrypt it, you just read the extension and save it again with its original extension.
    jawib's idea is kinda hard..
    You could change a file like file.ext to file.encoded.ext to file_encoded.ext instead.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    stringstreams!
    Code:
    #include <sstream>
    
    std::stringstream Stream;
    Stream << "dec_" << argv[1];
    
    Stream.str() //Get an std::string from it
    Stream.str().c_str() //Get a char pointer from it
    Works with integers etc which std::string operator + won't.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Manipulating Character arrays in functions.
    By kbro3 in forum C++ Programming
    Replies: 11
    Last Post: 08-16-2008, 02:24 AM
  2. Strings and Substrings using arrays
    By dcwang3 in forum C Programming
    Replies: 12
    Last Post: 02-18-2008, 07:28 AM
  3. copying strings to heap character arrays
    By SkyRaign in forum C++ Programming
    Replies: 4
    Last Post: 11-26-2006, 02:08 PM
  4. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  5. Storing strings in 2d char arrays problem
    By rainmanddw in forum C++ Programming
    Replies: 5
    Last Post: 10-22-2003, 05:41 PM