binary file

This is a discussion on binary file within the C++ Programming forums, part of the General Programming Boards category; I wrote a program that out puts a binary file. I have another program that reads in the binary file. ...

  1. #1
    dcdc
    Guest

    Unhappy binary file

    I wrote a program that out puts a binary file. I have another program that reads in the binary file. I cannot get back the original data. How would I get back to the original data.

    I am using the following to read the binay file

    char buffer[4];

    while(!infile.eof)
    {

    infile.read((char *) &z, sizeof(z));
    cout << z << endl;

    }

    ???? please help.
    I keep getting back garbage.

    Thanks.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Can you post a bit more code, in code tags. What is z?

    You shouldn't control the loop using eof() either, instead use the return code from the read function.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    dcdc
    Guest

    Smile

    z is a double.

    I wrote the binary file with this:

    outfile.write(((char *) &z),sizeof(z));

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Did you open it in binary mode?

    Can you post more code? Preferably something that is close to compilable.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    70
    keep in mind, binary isnt like textpad kinda stuff so what it returns may be differant than what ya put in

    -Tootles
    *-*
    "...since anyone who is anyone knows C..." -Peter Cellik

  6. #6
    dcdc
    Guest

    Smile

    Code:
    #include <iostream>
    #include <math.h>
    #include <fstream>
    #include <stdlib.h>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
    unsigned int x;
    double z;
    char buffer[4];
    ifstream infile("data.bin", ios::binary);
    infile.read(buffer,4);
    while(!infile.eof())
    
    	{
          infile.read(buffer,4);
         infile.read((char *) &z, sizeof(z));
          cout << z << endl;
    
        }
         
          
          infile.close(); 
    	  return 0;
    }

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here, I knocked this up as a quick example:
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main(void)
    {
      double  d1 = 10.5, d2 = 0;
    
      fstream myfile("out.dat", ios::out | ios::binary);
    
      if (!myfile)
      {
        perror("out.dat");
        return(0);
      }
    
      myfile.write((char *) &d1, sizeof(d1));
      myfile.close();
    
      myfile.open("out.dat", ios::in | ios::binary);
    
      if (!myfile)
      {
        perror("out.dat");
        return(0);
      }
    
      myfile.read((char *) &d2, sizeof(d1));
      myfile.close();
      cout << d2 << endl;
      return(0);
    }
    In your code, what's the 4 byte buffer all about?

    >>keep in mind, binary isnt like textpad kinda stuff so what it returns may be differant than what ya put in
    What are you talking about?!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    70
    Binary isnt plain text what you think it will return isnt always it, binary is more compressed (thats not thee word I want but you get the idea)so whats returned doesnt look like plain text (not supposed to)
    "...since anyone who is anyone knows C..." -Peter Cellik

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  2. Print file in binary mode
    By vikernes in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2006, 12:43 AM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21