|
| Help for Stream-copy File |
 |
Thu, 02 Nov 2006 13:26:19 GMT |
Hi,
I have try to copy file in other file with stream.
The destination file have 6 bit in plus.
My code:
ifstream myfile("c:\\1.pdf", ios::in|ios::binary|ios::ate);
byte * memblock;
int size;
//read size of file
size = myfile.tellg();
memblock = new char [size];
myfile.seekg (0, ios::beg);
//read file input
myfile.read (memblock, size);
myfile.close();
// make outfile
ofstream outfile ("c:\\2.pdf");
// write outfile
outfile.write(memblock,size);
outfile.close();
delete[] memblock;
When I go to compare the two file I have some difference in emtpy char.
|
| Post Reply
|
| Re: Help for Stream-copy File |
 |
5 Nov 2006 07:35:03 -0700 |
Ciccio <ciccio.unicoTOGLIMI@libero.it> wrote:
>The destination file have 6 bit in plus.
Sorry, I don't understand that statement.
Some suggestions:
(1) Always state which compiler and version you're
using. Turbo C++ for DOS, for Windows, etc.
(2) Always state what kind of program you're building.
DOS? Win16? Win32 console? Win32 GUI? Other?
(3)
>ifstream myfile("c:\\1.pdf", ios::in|ios::binary|ios::ate);
Always test for failure of a file open before trying to use
the file via seek/read/write.
(4)
>int size;
>//read size of file
>size = myfile.tellg();
(a) Always test for failure of a seek operation before
trying to use the new file position or the value
returned from a tellg operation.
(b) In a 16-bit program, an int may be too small.
(5)
>memblock = new char [size];
Always test for failure of new or malloc/realloc
before trying to use the space allocated.
(6)
>myfile.seekg (0, ios::beg);
Always test for failure of a seek operation before
trying to use the new file position.
(7)
>//read file input
>myfile.read (memblock, size);
Reading the entire file in one operation is usually
unadvisable. For very large files the memory demands
may be excessive. It's often better to read the file in
chunks or blocks. e.g. - 4096 bytes or multiples thereof.
(8)
>// make outfile
>ofstream outfile ("c:\\2.pdf");
(a) Always test for failure of a file open before trying to
use the file via seek/read/write.
(b) Since the input file is opened in binary mode you should
also open the output file in binary mode.
(9)
>// write outfile
>outfile.write(memblock,size);
>outfile.close();
Always test for failure of writes or file close of output files.
>When I go to compare the two file I have some difference ...
I'm not surprised. ;-)
- Wayne
|
| Post Reply
|
| Re: Help for Stream-copy File |
 |
5 Nov 2006 10:59:25 -0700 |
"Wayne A. King" <waking@idirect.com> wrote:
>(7)
>>//read file input
>>myfile.read (memblock, size);
Always test for failure of read/get file operations
before trying to use any retrieved data.
- Wayne
|
| Post Reply
|
|
|
|
|
|
|
|
|
|