Error in reading fom a binary file!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mohammed alhadi
    New Member
    • May 2011
    • 8

    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!

    Code:
    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_ifst ream<char,struc t std::char_trait s<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
    Last edited by Niheel; May 13 '11, 02:16 PM.
  • hype261
    New Member
    • Apr 2010
    • 207

    #2
    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

    Comment

    • mohammed alhadi
      New Member
      • May 2011
      • 8

      #3
      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.bi n", ios::in|ios::bi nary);


      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;

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        si is a single int. You have () instead of [].

        Comment

        • mohammed alhadi
          New Member
          • May 2011
          • 8

          #5
          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?!

          Comment

          • hype261
            New Member
            • Apr 2010
            • 207

            #6
            First you are not going back to the begining of the file after you figure out the file size.

            Code:
            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.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              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.

              Comment

              • mohammed alhadi
                New Member
                • May 2011
                • 8

                #8
                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;

                Comment

                • johny10151981
                  Top Contributor
                  • Jan 2010
                  • 1059

                  #9
                  simple reading method would be
                  Code:
                  //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
                  Code:
                  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:
                  Code:
                  file.read((char *)p, size-(size%sizeof int));
                  Last edited by johny10151981; Apr 18 '12, 05:03 AM. Reason: wrong code, I was thinking of malloc function

                  Comment

                  Working...