Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copying files from one directory to another 1

Status
Not open for further replies.

peaseblossom

IS-IT--Management
Mar 27, 2007
5
GB
Hi: a real newbie trying to write a utility that copies a file from 1 directory to another; I get a lot of error messages


#include <fstream>
#include <ios>
#include <iostream>
using std::cout;
int main() {
const char* from = "cod.fil";
std::string dir ("/home/Dad/hello/cut/");
const char* to ="12345";
std::string dest = dir + to;
std::ifstream infile(from, std::ios_base::binary);
std::eek:fstream outfile(dest, std::ios_base::binary);
outfile << infile.rdbuf();

cout<< dest;
}

The issue appears to revolve around the variable 'dest'in the ofstream line.'to' works. Could anybody advise where I am going wrong - and how to resolve? I have 4 manuals c++ manuals and they have yet to help!
Thanks!
 
> std::eek:fstream outfile(dest, std::ios_base::binary);
This should be
[tt]std::eek:fstream outfile(dest[blue].c_str()[/blue], std::ios_base::binary);[/tt]

Also, please use the [code][/code] tags when posting code, so
int main() {
const char* from = "cod.fil";
std::string dir ("/home/Dad/hello/cut/");
looks like
Code:
int main() {
    const char* from = "cod.fil";
    std::string  dir ("/home/Dad/hello/cut/");

--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Thank you very much for the response; however I now have an unusual problem in that when I change the 'to' variable to the last 6 characters in a text file I get a lot clutter at the end; thus my dest variable reads:
/home/Dad/hello/cut/78945P<@?????\
so the file created is not 78945P in the cut directory but 78945P"£$%%^$ or whatever - but in the cut directory at least!
I understand it is something to do with end of line but could anybody advise how to either set an end of line to the text file source or just select those characters(they will always start in the same position in the text file, and always 6 characters in length)
Thanks again
 
Perhaps use a better copying loop?
Code:
    char ch;
    while ( infile.get(ch) ) {
        outfile.put(ch);
    }

--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top