Thread: how to add variable to string??!

  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
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    4,633
    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, 03:45 PM
  3. Set string variable
    By Beaner in forum C Programming
    Replies: 3
    Last Post: 01-28-2006, 06: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, 04:21 PM