473,322 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 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 9047
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Nils Wogatzky | last post by:
Hi, I´ve got a problem with the iftream.read method. I´m reading out a binary file, but I receive wrong values if values are negative. m_File.read((char*)&help2,2); so, help2 is...
4
by: Clint Ruen | last post by:
Hello all, I have written out a data structure using the binary flag on an ofstream. The struct/class is something like this class SomeData { public: int data1;
12
by: Steven T. Hatton | last post by:
I know of a least one person who believes std::ifstream::read() and std::ofstream::write() are "mistakes". They seem to do the job I want done. What's wrong with them. This is the code I...
1
by: Alex Vinokur | last post by:
Testsuites "Comparative Performance Measurement. Reading file into string" at http://groups.google.com/group/perfo/msg/8273f4d1a05cfbd1 http://groups.google.com/group/sources/msg/27a9b6f91239c909...
4
by: Paul Malcomson | last post by:
I'm trying to finish implementing security on my database. I have secured the .mdb with a .mdw file as per the MS security FAQ with no problems. The last part of security that I need to...
2
by: Michael A. Covington | last post by:
I want to deploy a project in which the user is provided with a set of READ-ONLY files to use as templates. They will be in a directory to which the user can add files of his own. It's...
1
by: eiji | last post by:
Hi folks, I'm new to binary-file handling and try to work/learn playing around with MD5. Now everything works fine with small files, but when files get bigger(e.g. 28MB) this loop stops in the...
2
by: =?Utf-8?B?YW5rMmdv?= | last post by:
Thanks in advance for reading this. Let's say I have a file (file01) with this data in ASCII (ignore line col): line01 123abc line02 Header01 Starts blah var line03 detail01 000001...
1
by: Sergei Shelukhin | last post by:
Hi. I have the following problem; VC++ project, there's this code operation on 500Mb file via ifstream, POS_TYPE for this case is unsigned int which is 32-bit long. //dataFile.good() returns 1...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.