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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copying files in binary mode (ifstream/ofstream) 2

Status
Not open for further replies.

biot023

Programmer
Nov 8, 2001
403
0
0
GB
Hallo.
I need to copy files accross a network, so would be best, I imagine, opening one as an ifstream & reading it into an ofstream in binary mode.
Would copying all files in binary mode guarantee that the file would not be altered, or is there some wierd kind of ftp-type reason for copying files in different ways?
Also - could anyone show me a snippet of code that might be the best way to make this transfer? I've never even opened a file in binary mode before, let alone anything else...

Cheers,
Douglas JL

Common sense is what tells you the world is flat.
 
Yes, binary mode should do it. What may happen it you don't is the stream may map CR to CR/NL. This happens alot in moving files from a *nix to Windows machine but rarely if moving from *nix to *nix or Windows to Windows. However, it is better to be safe than sorry.

James P. Cottingham

When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour. That's relativity.
[tab][tab]Albert Einstein explaining his Theory of Relativity to a group of journalists.
 
Cheers, man - do you know if the stuff in the file's header would be unaltered?
I mean like date modified, etc.
Or would I have to set those manually?
And - sorry to keep blagging you with questions - is there a really simple way of performing the transfer of data?
Like:
while(!orig.eof())
copy<<orig;

Or something?
Cheers,
Douglas JL Common sense is what tells you the world is flat.
 
Just to finish the story off, the way I got the copy to work was as follows (I guess most people will know this):

ifstream source(&quot;file.tar.gz&quot;,ios::binary);
ofstream copy(&quot;copy_of_file.tar.gz&quot;,ios::binary);
copy<<source.rdbuf();

Cheers, Mr. Cottingham! :)

Douglas JL Common sense is what tells you the world is flat.
 
Note that ios::binary is from the old-style <iostream.h>, pre-STL library. The current STL library <iostream> uses ios_base::binary.
 
Hey guys. I have a question about this issue. Isn't 'fstream.h' library obsolete? I mean, can I work with files that have long names using this library. If I try to manipulate such a file, wouldn't I alter it's name? 'Cause as I know, there are newer libraries in CBuilder for working with files, but i don't know how to use them.
 
For the examples above, I used the following:

#include<fstream>

This is the updated version w/ the iostream classes in it.
Then the usual

using namespace std;

Cheers,

A salesman is a machine for turning coke into obnoxious arrogance.

Common sense is what tells you the world is flat.

 
I thing for copy files you should use CopyFile function and #include<windows.h>

the syntax is
CopyFile(oldpath, newpath, TRUE/FALSE);
The last parameter specifies if to fail if newpath already exist. If you want to copy across shared folders in network, you should specify full network path,
\\computername\folderpath\file (in c++ &quot;\\\\xxx\\xxx\\xxx&quot;)

Ion Filipski
1c.bmp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top