Copying files

So, I have a question about an easy implementation of copying files.

My first idea, was just to open two fstreams, and copy all the contents of one to the other. I now think this may be a painful task on large files, considering the speed of I/O.

So I just had another idea. Is it possible to have a pointer to both files, and just dereference the destination file, and have it equal the derefernced source file? Would this accomplish what I'm going for?

Sorry my file I/O experience is really slim. One of the reasons I'm doing this project
Last edited on
The speed of the write operation shouldn't be too bad just dont output to console.
Edit: Dont do that with the pointers.
Last edited on
So I just had another idea. Is it possible to have a pointer to both files, and just dereference the destination file, and have it equal the derefernced source file? Would this accomplish what I'm going for?

That's more or less equivalent to creating a hard link. This requires that the destination file is within the same file system and that the file system itself supports it.
However, it also follows that modifying the copy modifies the original.

I now think this may be a painful task on large files, considering the speed of I/O.

I/O speed is just fine with modern hardware. Copying a 700 MB file from my SSD to a SpinPoint F4 takes about three seconds for me (or rather, the appropriate read/write calls do).
Last edited on
@naraku
The speed of the write operation shouldn't be too bad just dont output to console


So even if I have a 10MB+ file, copying over won't be a big deal?

@Athar
However, it also follows that modifying the copy also modifies the original.


Let's say I have this:

1
2
3
4
5
6
fstream * sourcePtr = &sourceFile; //Whatever the source file stream is called

fstream * destPtr = &destFile; //Same thing

*destPtr = *sourcePtr;


Will this not work? I'm actually not sure how files are stored in memory, but I have the feeling it's similar to a container? In which case I'll just be getting the first block of the file? I really don't know here

EDIT:
Had it backwards, fixed now
Last edited on
It's easy to find out how it works:
http://www.cplusplus.com/reference/iostream/fstream/

And possibly this as well:
http://www.cplusplus.com/doc/tutorial/files/
Last edited on
Sorry I don't get how these help with copying over full files :/

And my idea of copying two dereferenced pointers didn't work. Got some errors in ios_base.h for some reason
Sorry I don't get how these help with copying over full files :/

What? They tell you how to use fstream.

And my idea of copying two dereferenced pointers didn't work.

Of course. Or did you see an operator= in the reference for fstream?
Of course. Or did you see an operator= in the reference for fstream?


Aah no I didn't. I guess that would explain it. I guess I assumed operator= was just always there.

To be honest, I've never played with operator overloading. But, would it be possible to overload = to make it work for copying contents of one file into another file? I'm already invested in this project, and don't wanna leave with nothing here haha.
But, would it be possible to overload = to make it work for copying contents of one file into another file?

Certainly, but whether that's a good idea is a different question.
If a file stream class provided an operator=, I'd probably expect it to open the same file at the same position, not to overwrite it with the data of another file.

Just use read() and write() to implement your file copy function.
Last edited on
So if you saw

fstreamObj1 = fstreamObj2

You wouldn't expect Obj1 to be overwritten with Obj2?
Sorry I don't get how these help with copying over full files :/


My link has an example of reading the entire contents of a file into a char array. Just write that array to a file instead of the console.

Perhaps something like this works:
1
2
3
4
5
6
7
8
9
10
ifstream inFile("in.txt", ios::binary);
ofstream outFile("out.txt", ios::binary);

while (inFile.good())  // Though perhaps this condition is wrong
{
 outFile.put(inFile.get());
}

inFile.close();
outFile.close();
Last edited on
Topic archived. No new replies allowed.