473,396 Members | 2,030 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,396 software developers and data experts.

Error in reading fom a binary file!

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!

Expand|Select|Wrap|Line Numbers
  1. int i;
  2. ifstream::pos_type size;
  3.  
  4.         ifstream pBinaryFile ("binaryfile.bin", ios::in|ios::binary|ios::ate); //other format to read from the binary file
  5.  
  6.  
  7.         if (!pBinaryFile)
  8.         {
  9.             printf("Unable to open file!");
  10.             return 1;
  11.         }
  12.  
  13.  
  14.         size = pBinaryFile.tellg(); //to get the size of the binary file in bytes
  15.         file.seekg (0, ios::beg); // and then go to the beginning of the file to read from it
  16.  
  17.         for ( counter=1; counter <= (size/sizeof(int)); counter++) 
  18.         // divided over sizeof because I want to read as long as the number of int values
  19.         {
  20.         fread(&i,sizeof(i),1,pBinaryFile);
  21.             printf("%d ",i);    
  22.  
  23.         }
  24.        cout<<endl;
  25.  
  26.        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
May 13 '11 #1
8 3913
hype261
207 100+
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
May 13 '11 #2
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;
May 13 '11 #3
weaknessforcats
9,208 Expert Mod 8TB
si is a single int. You have () instead of [].
May 13 '11 #4
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?!
May 13 '11 #5
hype261
207 100+
First you are not going back to the begining of the file after you figure out the file size.

Expand|Select|Wrap|Line Numbers
  1. file.seekg (0, ios::beg);
  2.  
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.
May 13 '11 #6
weaknessforcats
9,208 Expert Mod 8TB
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.
May 13 '11 #7
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;
May 14 '11 #8
johny10151981
1,059 1GB
simple reading method would be
Expand|Select|Wrap|Line Numbers
  1. //you did
  2. file.read((char *) &p, sizeof p);
  3. // but it would be
  4. file.read((char *)p, size); //size is the length of file. 
  5.  
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
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. file.read((char *)p, size-(size%sizeof int));
  2.  
Apr 18 '12 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
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...
2
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")>...
2
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...
6
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...
1
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; }...
8
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");
9
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...
2
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...
11
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...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
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...
0
isladogs
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...

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.