| | |
C fputc and fgetc vs C++ read and write
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2005
Posts: 148
Reputation:
Solved Threads: 6
Hello,
copying from one file to another is a simple matter.
In C it's something like this:
I'm wondering how this is done in C++
My idea is this:
I think this is very bad solution because of performance.
Can you suggest me any other way in C++ to achive this?
Thanks
copying from one file to another is a simple matter.
In C it's something like this:
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
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
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:
>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.
On the surface at least.
>I'm wondering how this is done in C++
A direct translation of C streams to C++ streams is:
C++ Syntax (Toggle Plain Text)
#include <fstream> int main() { ifstream in ( "infile" ); ofstream out ( "outfile" ); if ( in && out ) { char ch; while ( in.get ( ch ) ) out.put ( ch ); } }
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
•
•
Join Date: Aug 2005
Posts: 148
Reputation:
Solved Threads: 6
•
•
•
•
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:
>I think this is very bad solution because of performance.C++ Syntax (Toggle Plain Text)
#include <fstream> int main() { ifstream in ( "infile" ); ofstream out ( "outfile" ); if ( in && out ) { char ch; while ( in.get ( ch ) ) out.put ( ch ); } }
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?
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: STL iterators...what position am I?
- Next Thread: I Need A Willing C++ Programmer
Views: 9236 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






