Question about copying a file

closed account (2UD8vCM9)
Okay, so i'm trying to figure out how I would code copying a file that cannot be opened (eg. any file that isn't ANSII format).

I know that for a .txt or something like that I could simply do

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>
#include <string>

using namespace std;


int main()
{
	ifstream FileToCopy;
	ofstream CopiedFile;
	string linetocopy;
	FileToCopy.open("Test.txt");
	CopiedFile.open("Copy_Of_Test.txt");
	while (!FileToCopy.eof())
	{
		getline(FileToCopy,linetocopy);
		CopiedFile << linetocopy << endl;
	}
	FileToCopy.close();
	CopiedFile.close();

	return 0;
}



But could anyone help point me into the right direction of how I would copy a file's contents into a char buffer and copy it over to another file for files that can't be opened/read in notepad? (Example: a rar file or a .exe ) Not sure if that makes any sense, please let me know if I need to clarify.
Last edited on
Short Answer: Use Binary for your file input/output.

Assuming speed isn't your number 1 concern this way works well enough for me.

1
2
3
4
5
6
7
8
 	ofstream Output("Output2.mp3", ios::binary);
	ifstream Input("Output.mp3", ios::binary);

	//Make sure files exist etc

	Output << Input.rdbuf();

	//Clear Everything up  


http://www.cplusplus.com/reference/fstream/ifstream/rdbuf/
<< is overloaded it so it prints out the entire file from the file buffer pointer (rdbuf).
closed account (2UD8vCM9)
Thanks James for the clear and awesome response.

Next question I have:

Is there a way to just copy a part of that Input stream and copy it in chunks to the other file, or is what i'm trying not possible?

Edit: Nevermind I am reading the link you posted now thanks.
Last edited on
Topic archived. No new replies allowed.