Thread: Programming in Binary File..don't know where to start

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    34

    Programming in Binary File..don't know where to start

    Ok, I don't even know where to start with this, but here goes the problem. First off, this all has to be in a class called BinaryFile. I have to create an account number, and account balance that will be entered from an inFile. I then have to create an empty binary file with a size of 100 records, and then insert the valid records into that binary file with key%97. I then have to read the binary file sequentially and write the records in position, and then do the same randomly. Can someone start me off or show me what to do? Thanks.

  2. #2
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Well.. at least write the code for the class.

    After you have a class, or linked list, write the function to read and write an object to the file. Thats done with.. file I/O, theres tutorials everywhere.

    Code:
    int g_accountKey = 0;
    
    class BinaryFile
    {
    private:
      int m_accountNumber;
      double m_accountBalance;
      int m_accountKey;
    
    public:
      BinaryFile() : m_accountNumber(0), m_accountBalance(0), m_accountKey(++g_accountKey) {}
      BinaryFile(int p_accountNumber, double p_accountBalance) : m_accountNumber(p_accountNumber), m_accountBalance(p_accountBalance), m_accountKey(++g_accountKey) {}
    
      int GetKey() { return m_accountKey; }
    
      void Write() { //file I/O 
                          }
    };
    
    void WriteAll()
    {
    //do the 100 allocated part here
    }
    If you wanted to do this efficiently you would probably make a linked list (and a key variable in it, not global), and have a key count the nodes, and a method to do the file I/O, but a function and a class would work.

    You'll need to be reading and writting the right data, so you'll have to use a key or something to track them. If you were using C I/O you could figure out the position by sizeof(BinaryFile) * GetKey(); however in C++ you'd need to seek to position. Go to cplusplus for functions you can do with fstream.

    The code posted is for me to guess, and for you to find out, something as basic as the above might not even compile

    Oh and to do a random one you would simply make a blank BinaryFile object, and then out of your array of 100 BinaryFiles (using a vector) you use a rand to chooce one to assign. e.g. BinaryFile blankChoice = arrayBinaryFiles[rand % 100];. Or you could write a normal function that reads, but instead of using a key, uses a randomly generated number, etc.
    Last edited by Dae; 09-26-2005 at 07:35 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM