Hi All,
Can any body tell me how to fix the errors in this program? I am reading integer values from a binary file and displaying them! - int i;
-
ifstream::pos_type size;
-
-
ifstream pBinaryFile ("binaryfile.bin", ios::in|ios::binary|ios::ate); //other format to read from the binary file
-
-
-
if (!pBinaryFile)
-
{
-
printf("Unable to open file!");
-
return 1;
-
}
-
-
-
size = pBinaryFile.tellg(); //to get the size of the binary file in bytes
-
file.seekg (0, ios::beg); // and then go to the beginning of the file to read from it
-
-
for ( counter=1; counter <= (size/sizeof(int)); counter++)
-
// divided over sizeof because I want to read as long as the number of int values
-
{
-
fread(&i,sizeof(i),1,pBinaryFile);
-
printf("%d ",i);
-
-
}
-
cout<<endl;
-
-
fclose(pBinaryFile);
errors that I have:
error C2040: 'pBinaryFile' : 'class std::basic_ifstream<char,struct std::char_traits<char> >' differs in levels of indirection from 'struct _iobuf *'
error C2228: left of '.tellg' must have class/struct/union type
error C2065: 'file' : undeclared identifier
error C2228: left of '.seekg' must have class/struct/union type
8 3913
Are you sure you are including the right header file for the ifstream?
I believe it is defined in <fstream>
Also you need to replace file with pBinaryFile at line 15
Thank u Hype 261, I have changed the operation of reading from a file to an array as follows: but it give me rabish!
int i;
fstream file ("binaryfile.bin", ios::in|ios::binary);
if (!file)
{
printf("Unable to open file!");
return 1;
}
file.seekg(0, ios::end); // position get-ptr 0 bytes from end
ios::pos_type size = file.tellg(); // get-ptr position is now same as file size
int *p;
int si=int(size/sizeof(int));
p= new int[si];
file.read((char *) &p, sizeof p);
for(i=0; i<si; i++) // show values read from file
cout << p[i] << " ";
file.close();
return 0;
si is a single int. You have () instead of [].
Hi weaknessforcats, si is just the number of integer values in he binary file ... I calculated it by taking the number of bytes in the file and dividing it by the size of int... and then cast it to int .... don't u think it is correct?!
First you are not going back to the begining of the file after you figure out the file size. -
file.seekg (0, ios::beg);
-
Secondly I am not entirely sure that the file.read code is going to work correcly. Generally when I do file manipulation I use getline and stringstreams to make sure the data is good.
You should be calculating the number of bytes in a file and then reading these bytes into an array of the correct size.
Then you typecast the array name to the correct type. Of course, as with all reads, this presupposes you already know the layout of the file.
So if there are 1000 byes then read the bytes into a char array of 1000 char. Then typecast the array address as an int*. This would give you 250 int.
I made this but it also doesn't work, it give me big numbers!
file.seekg(0, ios::end);
ios::pos_type size = file.tellg();
file.seekg (0, ios::beg);
char *p;
p= new char[size];
int *q;
q=new int [size/sizeof(int)];
q=(int*)&p;
for(i=0; i<size; i++)
cout << q[i] << " ";
file.close();
return 0;
simple reading method would be -
//you did
-
file.read((char *) &p, sizeof p);
-
// but it would be
-
file.read((char *)p, size); //size is the length of file.
-
but yet it has a little bug
say what if size%sizeof int!=0? which mean you have some extra bytes. it would cause you segmentation fault. to fix this issue.
you can allocate your int array as - p = new int[size/sizeof(int) + (size%(sizeof int)!=0)];
if the modulus == 0 then it wont create an extra 4 bytes otherwise it would or you can skip the last extra bytes.
this way: -
file.read((char *)p, size-(size%sizeof int));
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: poifull |
last post by:
Hi All,
What is the proper way to read a binary file into a byte?
I am using BinaryReader to read from a Stream and call the ReadByte method
of the BinaryReader object.
The method I'm using...
|
by: Kel Good |
last post by:
I have a component which I want to register for COM interop. At the top of
my code file I have the following lines.
Imports System.Reflection
<Assembly: AssemblyKeyFileAttribute("myKey.snk")>...
|
by: craigwhunt |
last post by:
Hi all,
I am deploying my ASP.NET 2.0 bits. Everything works fine in dev, but when I
move it to test I get the following:
CS1548: Cryptographic failure while signing assembly...
|
by: al jones |
last post by:
I picked a good project to try to learn to use VS - and up till now
everything has worked the way I expect. The code is from a VS.net class,
which I picked up on the web, from which I've extracted...
|
by: RML |
last post by:
Hi,
My VS 2005 VB app reads a binary file that was created by a VS 2005 VC++
WinCE app. The VC++ app writes the following structure to the file.
typedef struct {
short ID;
TCHAR Num;
}...
|
by: Shalaka Joshi |
last post by:
Hi,
I have binary file say, "test.bin". I write "FF" in the file and expect
my code to read 255 for me.
char * lbuf;
int lreadBytes ;
long lData;
FILE *pFile = fopen ("c:\\testbin","rb");
|
by: Use*n*x |
last post by:
Hello,
I have a binary file (image file) and am reading 4-bytes at a time. The
File size is 63,480,320 bytes. My assumption is that if I loop through
this file reading 4 bytes at a time, I...
|
by: Ben |
last post by:
Hi
We have setup a Strong Name for our Applications, but are having problems
with the essembly throwing an error:
Error creating assembly manifest: Error reading key file...
|
by: Freddy Coal |
last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all
the file in a String.
I make this function for make that:
'--------------------------
Public Shared Function...
|
by: Craftkiller |
last post by:
=========Program compiles on both windows and linux===========
Greetings, I am currently working on an encrpytion program that will take a file and a password string and flip the bits based on the...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
| |