how to add variable to string??!

This is a discussion on how to add variable to string??! within the C++ Programming forums, part of the General Programming Boards category; Hi, am editing this C++ code.. It will get the folder directory.. then i want to copy a file to ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    34

    how to add variable to string??!

    Hi,
    am editing this C++ code..
    It will get the folder directory.. then i want to copy a file to sub folder in that directory.

    Code:
    #include <fstream>
    #include <string>
    #include <iostream>
    using namespace std;
    
    bool copyFile (const char SRC[], const char DEST[])
    {
        std::ifstream src; // the source file
        std::ofstream dest; // the destination file
    
        src.open (SRC, std::ios::binary); // open in binary to prevent jargon at the end of the buffer
        dest.open (DEST, std::ios::binary); // same again, binary
        if (!src.is_open() || !dest.is_open())
            return false; // could not be copied
    
        dest << src.rdbuf (); // copy the content
        dest.close (); // close destination file
        src.close (); // close source file
    
        return true; // file copied successfully
    }
    
    int main ()
    {
        char* libvar;
        string filepath = "\\new\\input2.txt";
    
       // Get the value of the LIB environment variable.
       libvar = getenv( "LIB" ); // C4996
       // Note: getenv is deprecated; consider using getenv_s instead
    
       if( libvar != NULL )
          printf( "Original LIB variable is: %s\n", libvar );
        ifstream f1 ("input.txt",fstream::binary);
        //ofstream f2 (libvar, "\\new\\input2.txt", fstream::trunc|fstream::binary);
        //f2<<f1.rdbuf();
    
       // Attempt to change path. Note that this only affects the environment
       // variable of the current process. The command processor's
       // environment is not changed.
       // Note: _putenv is deprecated; consider using putenv_s instead
    
        if (!copyFile ("input.txt", libvar + "\\new\\input2.txt"))
            std::cout << "File could not be copied successfully";
        else
            std::cout << "File copied successfully!";
    
        std::cin.get (); // pause for input
        //return EXIT_SUCCESS; // program was executed successfully
    
       return 0;
    }
    i tried it but always gets errors
    Code:
    error C2110: cannot add two pointers
    Am using VC++ 6.0

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    There are a few ways, for example:
    Code:
    std::string libvar(getenv("LIB"));
    
    // ...
    
    if (!copyFile("input.txt", (libvar + "\\new\\input2.txt").c_str()))
    Quote Originally Posted by bakri
    Am using VC++ 6.0
    Why are you not using a more recently released compiler?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    34
    i used
    Code:
    std::libvar(getenv("LIB"));
    .. i forget string


    i got VC++ 2010 without FMC
    thanx 4 help LaserLight
    Last edited by bakri; 04-30-2012 at 07:41 AM.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    34
    i got problem..
    Yes its compiled but closed with error.
    this is new code:

    Code:
    #include <fstream>
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    bool copyFile (const char SRC[], const char DEST[])
    {
        std::ifstream src; // the source file
        std::ofstream dest; // the destination file
        src.open (SRC, std::ios::binary); // open in binary to prevent jargon at the end of the buffer
        dest.open (DEST, std::ios::binary); // same again, binary
        if (!src.is_open() || !dest.is_open())
            return false; // could not be copied
    
        dest << src.rdbuf (); // copy the content
        dest.close (); // close destination file
        src.close (); // close source file
        return true; // file copied successfully
    }
    
    int main ()
    {
    
       // Get the value of the LIB environment variable.
       //libvar = getenv( "LIB" ); // C4996
        std::string libvar(getenv("LIB"));
    
      if(libvar.c_str() != NULL )
          printf( "Original LIB variable is: %s\n", libvar );
    
        if (!copyFile("input.txt", (libvar + "\\new\\input2.txt").c_str()))
            std::cout << "File could not be copied successfully";
        else
            std::cout << "File copied successfully!";
        std::cin.get (); // pause for input
       return 0;
    }

    And this is the error:

    Code:
    cp.exe has encountered a problem and needs to close.
    We are sorry for the inconvenience.
    Last edited by bakri; 04-30-2012 at 07:55 AM.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,753
    You can not print a C++ std::string with printf(). Use the C++ streams instead. Also you shouldn't be using the C++ extraction and insertion operators << >> when reading and writing binary files. You should be using the read and write member functions.

    Jim
    Last edited by jimblumberg; 04-30-2012 at 08:34 AM.

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    34
    ahaa.. am new in c++ and like to do tutorials.. now i got it
    am downloading and writing the tutorials from books and overnet because no one near me know much in c/c++

    thanks JIM.. you solved my error

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable In SQL String
    By mcertini in forum C++ Programming
    Replies: 3
    Last Post: 03-24-2011, 02:24 AM
  2. Variable In SQL String
    By mcertini in forum C++ Programming
    Replies: 1
    Last Post: 02-20-2011, 02:45 PM
  3. Set string variable
    By Beaner in forum C Programming
    Replies: 3
    Last Post: 01-28-2006, 05:15 PM
  4. Replies: 12
    Last Post: 10-14-2003, 10:17 AM
  5. string variable used as a variable...
    By Supar in forum C++ Programming
    Replies: 13
    Last Post: 03-29-2003, 03:21 PM

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