471,056 Members | 1,558 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,056 software developers and data experts.

question about ifstream::read()

I am having a heck of a time using the ifstream::read() method.
I am using Visual Studio 6.0, and my intelisense indicates that read
has an overload of:
ifstream::read(unsigned char*, int _n)

A lot of the code samples I have googled up show this method being
called being called with an unsigned char pointer as well. However
whenever I run my code:

#define BYTE unsigned char
....
int size;
BYTE* buffer;
....
std::ifstream inputFile("C:\\test.bin", std::ios::in |
std::ios::binary);
if (inputFile.is_open())
{
inputFile.seekg(0, std::ios::end);
size = inputFile.tellg();
buffer = new BYTE[size];
inputFile.read(buffer, size);
....

I get the error:
error C2664: 'read' : cannot convert parameter 1 from 'unsigned char
*' to 'char *' Types pointed to are unrelated; conversion requires
reinterpret_cast, C-style cast or function-style cast

If I cast buffer as a char* then my results are incorrect. Can
someone please tell me what I am doing wrong?

-John

May 11 '07 #1
4 8736
On May 12, 12:26 am, "supe...@gmail.com" <supe...@gmail.comwrote:
I am having a heck of a time using the ifstream::read() method.
I am using Visual Studio 6.0, and my intelisense indicates that read
has an overload of:
ifstream::read(unsigned char*, int _n)

A lot of the code samples I have googled up show this method being
called being called with an unsigned char pointer as well. However
whenever I run my code:

#define BYTE unsigned char
...
int size;
BYTE* buffer;
...
std::ifstream inputFile("C:\\test.bin", std::ios::in |
std::ios::binary);
if (inputFile.is_open())
{
inputFile.seekg(0, std::ios::end);
size = inputFile.tellg();
buffer = new BYTE[size];
inputFile.read(buffer, size);
...

I get the error:
error C2664: 'read' : cannot convert parameter 1 from 'unsigned char
*' to 'char *' Types pointed to are unrelated; conversion requires
reinterpret_cast, C-style cast or function-style cast

If I cast buffer as a char* then my results are incorrect. Can
someone please tell me what I am doing wrong?
inputFile.read( reinterpret_cast<char*>(buffer), size);

gives you the wrong answer ?

May 11 '07 #2
On May 11, 9:33 am, Gianni Mariani <gi3nos...@mariani.wswrote:
On May 12, 12:26 am, "supe...@gmail.com" <supe...@gmail.comwrote:


I am having a heck of a time using the ifstream::read() method.
I am using Visual Studio 6.0, and my intelisense indicates that read
has an overload of:
ifstream::read(unsigned char*, int _n)
A lot of the code samples I have googled up show this method being
called being called with an unsigned char pointer as well. However
whenever I run my code:
#define BYTE unsigned char
...
int size;
BYTE* buffer;
...
std::ifstream inputFile("C:\\test.bin", std::ios::in |
std::ios::binary);
if (inputFile.is_open())
{
inputFile.seekg(0, std::ios::end);
size = inputFile.tellg();
buffer = new BYTE[size];
inputFile.read(buffer, size);
...
I get the error:
error C2664: 'read' : cannot convert parameter 1 from 'unsigned char
*' to 'char *' Types pointed to are unrelated; conversion requires
reinterpret_cast, C-style cast or function-style cast
If I cast buffer as a char* then my results are incorrect. Can
someone please tell me what I am doing wrong?

inputFile.read( reinterpret_cast<char*>(buffer), size);

gives you the wrong answer ?- Hide quoted text -

- Show quoted text -
I want to say "yes", but I suppose there could be an error elsewhere
that is giving me a bad or wrong value. Should I get the expected
value when using a reinterpret_cast<char*>?

May 11 '07 #3
Well after diging around a little more it looks like the buffer is
completly filled with the hex code "CD"
It is the same size as the file, but only contains that single hex
pattern.
Does anyone have any idea why this would happen?

May 11 '07 #4
On May 11, 11:10 am, "supe...@gmail.com" <supe...@gmail.comwrote:
Well after diging around a little more it looks like the buffer is
completly filled with the hex code "CD"
It is the same size as the file, but only contains that single hex
pattern.
Does anyone have any idea why this would happen?
Never mind, I forgot to reset the get pointer. Adding the line
seekg(0) did the trick!

May 11 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Nils Wogatzky | last post: by
12 posts views Thread by Steven T. Hatton | last post: by
1 post views Thread by Alex Vinokur | last post: by
2 posts views Thread by Michael A. Covington | last post: by
1 post views Thread by eiji | last post: by
2 posts views Thread by =?Utf-8?B?YW5rMmdv?= | last post: by
1 post views Thread by Sergei Shelukhin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.