Hello,
copying from one file to another is a simple matter.
In C it's something like this:

int main(void)
{
	FILE * fp1, * fp2 ;
	int buff;

	fp1 = fopen ("name", "rb");
	fp2 = fopen ("name", "wb");

	while ( (buff = fgetc(fp1)) != EOF)
	{
		fputc(buff, fp2);
	}

}

I'm wondering how this is done in C++
My idea is this:

int main()
{
	int buf;

	ifstream fp_in("my.txt", ios_base::binary | ios_base::in );
	ofstream fp_out("my_copy.txt",	ios_base::binary | ios_base::out);

	while (fp_in.read ( (char*) &buf, sizeof buf ))
		fp_out.write ( (char*) &buf, sizeof buf );
}

I think this is very bad solution because of performance.
Can you suggest me any other way in C++ to achive this?

Thanks

Recommended Answers

All 4 Replies

only write out the number of bytes read. At some point (at end-of-file) the number of bytes read will be less than the buffer size. This would go much faster if you read/write with larger buffer size -- say 255 characters.

int main()
{
	char buf[255];

	ifstream fp_in("my.txt", ios_base::binary | ios_base::in );
	ofstream fp_out("my_copy.txt",	ios_base::binary | ios_base::out);

	while (fp_in.read ( buf, sizeof buf ))
		fp_out.write (buf, fp_in.gcount() );
}

>copying from one file to another is a simple matter.
On the surface at least.

>I'm wondering how this is done in C++
A direct translation of C streams to C++ streams is:

#include <fstream>

int main()
{
  ifstream in ( "infile" );
  ofstream out ( "outfile" );

  if ( in && out ) {
    char ch;

    while ( in.get ( ch ) )
      out.put ( ch );
  }
}

>I think this is very bad solution because of performance.
No, it's a very bad solution because it's a very bad solution. ;) You use an int when you should be using a char, to be consistent with the C program, or an array of char, to do block copies. You'll find that people think a character by character copy (such as the one above) is expensive because they think that each character is read and written individually, thus causing extra device operations. In reality, the stream will almost always buffer the data in the most efficient way possible to avoid device reads just as you would if you copied blocks manually. Only complain about performance if you can prove that there's actually a bottleneck. ;)

>copying from one file to another is a simple matter.
On the surface at least.

>I'm wondering how this is done in C++
A direct translation of C streams to C++ streams is:

#include <fstream>

int main()
{
  ifstream in ( "infile" );
  ofstream out ( "outfile" );

  if ( in && out ) {
    char ch;

    while ( in.get ( ch ) )
      out.put ( ch );
  }
}

>I think this is very bad solution because of performance.
No, it's a very bad solution because it's a very bad solution. ;) You use an int when you should be using a char, to be consistent with the C program, or an array of char, to do block copies. You'll find that people think a character by character copy (such as the one above) is expensive because they think that each character is read and written individually, thus causing extra device operations. In reality, the stream will almost always buffer the data in the most efficient way possible to avoid device reads just as you would if you copied blocks manually. Only complain about performance if you can prove that there's actually a bottleneck. ;)

Great, thanks, I was thinking about get and put, but I wasn't sure about text and binary mode. In C implementation with fputc and fgetc I opened files in binary mode, so I wanted to use binary mode also in C++ implementation... Or maybe that's not matter here?

>I was thinking about get and put, but I wasn't sure about text and binary mode
It doesn't matter in this case.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.