Hello,
Please look the code below where I am reading the first 2,2,4 bytes
from a binary file using two methods.
I am getting a wierd (wrong) output with ifstream and memcpy method,
but get the correct output with CFile read.
I am trying to get the values 1400 1050 and 2014 from the binary file.
Can anybody point out the mistake in the following code?
int main ()
{
ifstream myStream;
filedesc fd;
char sz2[30];
short i = 0;
short j = 0;
//Start Method one
fd = _open("my_binary_file.inf", _O_RDONLY, _O_BINARY);
myStream.clear();
myStream.attach(fd);
myStream.read(sz2, 20);
memcpy( &i, &sz2[0], 2 );
printf("x value is %d \n", i);
memcpy( &i, &sz2[2], 2 );
printf("y value is is %d \n", i);
memcpy( &i, &sz2[4], 4 );
printf("Version is %d \n", i);
myStream.close();
myStream.clear(); // reset eof state
//End Method one
//Start Method two
CFile file("my_binary_file.inf", CFile::modeRead);
file.Read((LPSTR)&j, 2);
printf("x value is %d \n", j);
file.Read((LPSTR)&j, 2);
printf("y value is %d \n", j);
file.Read((LPSTR)&j, 4);
printf("Version is %d \n", j);
file.Close();
//End Method two
return 0;
} 3 4068
sredd01 wrote: Please look the code below where I am reading the first 2,2,4 bytes from a binary file using two methods.
Both methods are non-standard. You should consider posting to a newsgroup
dedicated to your platform, since those are probably platform-specific, or
to a newsgroup for your compiler in case they are compiler-specific.
I am getting a wierd (wrong) output with ifstream and memcpy method,
Perhaps you shouldn't use memcpy...
but get the correct output with CFile read. I am trying to get the values 1400 1050 and 2014 from the binary file.
Can anybody point out the mistake in the following code?
int main () { ifstream myStream; filedesc fd; char sz2[30]; short i = 0; short j = 0;
//Start Method one fd = _open("my_binary_file.inf", _O_RDONLY, _O_BINARY); myStream.clear(); myStream.attach(fd);
I am unaware of any 'attach' members in 'ifstream'. Why don't you simply
open the stream instead of this?
myStream.open("my_binary_file.inf", ios::binary);
myStream.read(sz2, 20); memcpy( &i, &sz2[0], 2 ); printf("x value is %d \n", i); memcpy( &i, &sz2[2], 2 ); printf("y value is is %d \n", i); memcpy( &i, &sz2[4], 4 ); printf("Version is %d \n", i);
This is rather strange. Why don't you simply read the fields are they are
intended (and in order to compare to the CFile method):
myStream.read(reinterpret_cast<char*>(&i), sizeof(short));
??? myStream.close(); myStream.clear(); // reset eof state
//End Method one
//Start Method two
CFile file("my_binary_file.inf", CFile::modeRead);
file.Read((LPSTR)&j, 2); printf("x value is %d \n", j); file.Read((LPSTR)&j, 2); printf("y value is %d \n", j); file.Read((LPSTR)&j, 4); printf("Version is %d \n", j);
file.Close();
//End Method two return 0;
}
V
"sredd01" <rs*****@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com... myStream.read(sz2, 20); memcpy( &i, &sz2[0], 2 ); printf("x value is %d \n", i); memcpy( &i, &sz2[2], 2 ); printf("y value is is %d \n", i); memcpy( &i, &sz2[4], 4 ); printf("Version is %d \n", i);
This is implementation-specific. The size of i and j are as least 2, but
may not be big enough to hold four bytes.
Thanks a lot for your help.
I figured it out. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: wtnt |
last post by:
Hello.
I've searched all over and haven't seen another thread with this
problem. Please bear with me as I try to explain. thanks. :)
I have some programs that need to be cross-platform...
|
by: Dr. Len |
last post by:
Hi all!
Given that I have a binary file which contains 4-byte integer values
in sequence and I know in advance how many there are, does the C++
standard have any algorithm or like method to read...
|
by: Jacek Dziedzic |
last post by:
Hi!
Consider the following program
#include <fstream>
#include <iostream>
using namespace std;
int main() {
ifstream in("test.txt");
|
by: shawn |
last post by:
Hi All,
Am using MSVC6 compiler on a WinXP machine.
Am trying to read a file using std::ofstream;
the problem is that Tofstream.is_open() fails and Tifstream.rdstate()
returns "2" which...
|
by: Hamburgpear |
last post by:
Dear All,
Is it possible to reset the value of xxx.peek() after it reachs EOF ?
Regards
HP
|
by: farseer |
last post by:
Hi,
Hi, i am reading the content of a binary file, enoding as base64 and
writing to an output file. After noticing that my output file seems to
be truncated, i created the following simple test...
|
by: toton |
last post by:
Hi,
I want to unread a few things while I am reading a file. One
solution I know is putback the characters read to the buffer.
But as I need frequent moving file pointer , to a few steps back, I...
|
by: SantaClaus |
last post by:
Hi all,
Im getting an exception in the middle of reading a file and here is my code std::ifstream inf;
const int bufSize=1024;
char tbuffer;
std::string filex = path; // may be C:\test.txt
const...
|
by: James Kanze |
last post by:
On 11 avr, 17:44, "mc" <mc_r...@yahoo.comwrote:
OK. If the actual format is well documented, that's half the
battle won already.
Note, however, that reading a float as an int is still very...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |