question about ifstream::read()

S

supemoy

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
 
G

Gianni Mariani

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 ?
 
S

supemoy

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*>?
 
S

supemoy

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?
 
S

supemoy

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

binary file to CString 2
ifstream 5
ifstream 1
Can ifstream read file more than 2G? 4
multi-thread and ifstream 1
ifstream::read Question 5
Deriving from ifstream 3
ifstream character read problem 4

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top