×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Copying files from one directory to another

Copying files from one directory to another

Copying files from one directory to another

(OP)
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::ofstream 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!

RE: Copying files from one directory to another

> std::ofstream outfile(dest, std::ios_base::binary);
This should be
std::ofstream outfile(dest.c_str(), std::ios_base::binary);

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.

RE: Copying files from one directory to another

(OP)
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

RE: Copying files from one directory to another

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close