C fputc and fgetc vs C++ read and write

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2005
Posts: 148
Reputation: Micko is on a distinguished road 
Solved Threads: 6
Micko Micko is offline Offline
Junior Poster

C fputc and fgetc vs C++ read and write

 
0
  #1
Aug 31st, 2005
Hello,
copying from one file to another is a simple matter.
In C it's something like this:

  1.  
  2. int main(void)
  3. {
  4. FILE * fp1, * fp2 ;
  5. int buff;
  6.  
  7. fp1 = fopen ("name", "rb");
  8. fp2 = fopen ("name", "wb");
  9.  
  10. while ( (buff = fgetc(fp1)) != EOF)
  11. {
  12. fputc(buff, fp2);
  13. }
  14.  
  15. }

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

  1. int main()
  2. {
  3. int buf;
  4.  
  5. ifstream fp_in("my.txt", ios_base::binary | ios_base::in );
  6. ofstream fp_out("my_copy.txt", ios_base::binary | ios_base::out);
  7.  
  8. while (fp_in.read ( (char*) &buf, sizeof buf ))
  9. fp_out.write ( (char*) &buf, sizeof buf );
  10. }

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

Thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,648
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1498
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: C fputc and fgetc vs C++ read and write

 
0
  #2
Aug 31st, 2005
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() );
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: C fputc and fgetc vs C++ read and write

 
0
  #3
Aug 31st, 2005
>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:
  1. #include <fstream>
  2.  
  3. int main()
  4. {
  5. ifstream in ( "infile" );
  6. ofstream out ( "outfile" );
  7.  
  8. if ( in && out ) {
  9. char ch;
  10.  
  11. while ( in.get ( ch ) )
  12. out.put ( ch );
  13. }
  14. }
>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.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 148
Reputation: Micko is on a distinguished road 
Solved Threads: 6
Micko Micko is offline Offline
Junior Poster

Re: C fputc and fgetc vs C++ read and write

 
0
  #4
Aug 31st, 2005
Originally Posted by Narue
>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:
  1. #include <fstream>
  2.  
  3. int main()
  4. {
  5. ifstream in ( "infile" );
  6. ofstream out ( "outfile" );
  7.  
  8. if ( in && out ) {
  9. char ch;
  10.  
  11. while ( in.get ( ch ) )
  12. out.put ( ch );
  13. }
  14. }
>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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: C fputc and fgetc vs C++ read and write

 
0
  #5
Sep 1st, 2005
>I was thinking about get and put, but I wasn't sure about text and binary mode
It doesn't matter in this case.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 9236 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC