Overloaded function

I wrote a piece of code like this in order to read a file and copy the content into another file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
using namespace std;
void copy(char* src, char* dest)
{
	FILE *source = fopen(src, "rb");
	FILE *destination = fopen(dest, "wb");
	unsigned char buf[1000];
	int i;
	do
	{
		i = fread(buf, 1, 1000, source);
		fwrite(buf, 1, i, destination);
	} while (i == 1000);
	fclose(source);
	fclose(destination);
}
int main()
{
	copy("E:/input.txt", "E:/output.txt");
}

When I compiled, the compiler said: no instance of overloaded function "copy" matches the argument list and 'void copy(char *,char *)': cannot convert argument 1 from 'const char [13]' to 'char *'
What did I go wrong?
Last edited on
In C++, the type of a narrow string literal is 'array of const char'
ergo: 'cannot convert argument 1 from 'const char [13]' to 'char *' etc.

Rewrite as:
1
2
// void copy(char* src, char* dest)
void copy( const char* src, const char* dest )


Using the C++ streams library, the code becomes shorter, sweeter, and correct:
1
2
3
4
#include <fstream>

bool copy_file( const char* srce_path, const char* dest_path )
{ return bool( std::ofstream(dest_path) << std::ifstream(srce_path).rdbuf() ) ; }


Or, to copy files in binary mode:
1
2
bool copy_file( const char* srce_path, const char* dest_path )
{ return bool( std::ofstream( dest_path, std::ios::binary ) << std::ifstream( srce_path, std::ios::binary ).rdbuf() ) ; }


Last edited on
Hello thinhphucvang,

So which version of copy are you trying to use?

There is the copy function that you wrote and want to call and the "std::copy" that the compiler is trying to call.

I was wondering why you have C++ header files and you are using C code to work with your files?

This would work better for you:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>
#include <fstream>
#include <chrono>
#include <thread>

//using namespace std;  // <--- Best not to use.

void copyFile(std::string src, std::string dest)  // <--- Changed Name to avoid any confilict wit "std::copy()".
{
	std::string line;
	const std::string inFileName{ "input.txt" };
	const std::string outFileName{ "output.txt" };

	std::ifstream inFile(inFileName);

	if (!inFile)
	{
		std::cout << "\n File " << inFileName << " did not open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread".
		/*return 1;*/  exit(1);  // If not in "main".
	}

	std::ofstream outFile(outFileName, std::ios::trunc | std::ios::ate);

	if (!outFile)
	{
		std::cout << "\n File " << outFileName << " did not open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread".
		/*return 1;*/  exit(1);  // If not in "main".
	}

	while (std::getline(inFile, line))
	{
		outFile << line << std::endl;
	}

	inFile.close();
	outFile.close();
}

int main()
{
	copyFile("E:/input.txt", "E:/output.txt");

	return 0;
}


Hope that helps,

Andy
I should take enough sleep or drink coffee if I'm about to code.
Is it legal if I treated the name of the file as character array?
Like this:
1
2
3
char f1[] = "E:/input.txt";
char f2[] = "E:/output.txt";
copy(f1, f2);
Last edited on
By the way, I have edited the code:
1
2
3
4
5
6
void copy(char* src,  char* dest)
{
	ifstream in(src, ifstream::binary);
	ofstream out(dest, ofstream::binary);
	out << in.rdbuf();
}
> Is it legal if I treated the name of the file as character array?

Yes; but still not ideal, not const-correct.
There is no reason to insist that file names bust be consisting of modifiable characters.

Write it as void copy( const char* src, const char* dest )
Got it, thanks
Topic archived. No new replies allowed.