Connecting Tech Pros Worldwide Forums | Help | Site Map

question about ifstream::read()

supemoy@gmail.com
Guest
 
Posts: n/a
#1: May 11 '07
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


Gianni Mariani
Guest
 
Posts: n/a
#2: May 11 '07

re: question about ifstream::read()


On May 12, 12:26 am, "supe...@gmail.com" <supe...@gmail.comwrote:
Quote:
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 ?



supemoy@gmail.com
Guest
 
Posts: n/a
#3: May 11 '07

re: question about ifstream::read()


On May 11, 9:33 am, Gianni Mariani <gi3nos...@mariani.wswrote:
Quote:
On May 12, 12:26 am, "supe...@gmail.com" <supe...@gmail.comwrote:
>
>
>
>
>
Quote:
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)
>
Quote:
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:
>
Quote:
#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);
...
>
Quote:
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
>
Quote:
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*>?

supemoy@gmail.com
Guest
 
Posts: n/a
#4: May 11 '07

re: question about ifstream::read()


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?

supemoy@gmail.com
Guest
 
Posts: n/a
#5: May 11 '07

re: question about ifstream::read()


On May 11, 11:10 am, "supe...@gmail.com" <supe...@gmail.comwrote:
Quote:
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!

Closed Thread