Thread: reading binary file

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    26

    reading binary file

    I got the program in my last thread working, now I'm trying to get a program to read the binary file which it created in the last one. My only problem is that it won't read. At all.

    NOTE:
    I'm working on an old compiler. Saying 'put functions here blahblah' 'dont use.h' doesnt help at all, currently the code below compiles, there are 0 syntax errors.

    Code:
    #include <fstream.h>
    #include <stdlib.h>
    
    struct employee
    {
    	int id;
    	int salary;
    	char firstname[11];
    	char lastname[11];
    };
    
    
    int main (void)
    {
    fstream fsbin,
            fout;
    
    fsbin.open("hwk07.bin", ios::in);
    fout.open("hwk07out.txt", ios::out);
    
    
    
    void readwritebin (fstream&, fstream&);
    
    readwritebin (fsbin, fout);
    
    
    return 0;
    }
    
    void readwritebin (fstream& fsbin, fstream& fout)
    {
    employee worker = {0},
    labor[100] = {0};
    
    int idNum = 1;
    
    
    while (idNum != 0)
     {
      char id[4];
      cout << "\nPlease enter an id number, or '0' to exit: ";
      cin >> idNum;
    
    	if (idNum != 0)
    	{
    	 fsbin.seekg(idNum * sizeof(employee), ios::beg);
    	 fsbin.read((char*)&worker.id, sizeof(int));
    	 cout << "\nThe data for employee " << idNum << "is: \n"
              << worker.id << "\t" << worker.firstname << "\t"
      		  << worker.lastname << "\t" << worker.salary << "\t"
    		  << (worker.salary / 12.0) << "\n";
    	} //if
      } // while
    } // readwritebin

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Are you saying that puting this function prototype outside before the main won't fix anything?
    void readwritebin (fstream&, fstream&);
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    I just glanced at your code. First of all you need to open the binary file in binary mode. The default mode is text. Use the flag ios::binary. You can have multiple flags together by or'ing them. For example:

    ios::binary | ios::out

    Also, take out the function prototype from within your main function. Place this above main like so:

    Code:
    #include <fstream.h>
    #include <stdlib.h>
    
    struct employee
    {
    int id;
    int salary;
    char firstname[11];
    char lastname[11];
    };
    
    // Function Prototypes here:
    void readwritebin (fsbin, fout);
    
    int main (void)
    {
    fstream fsbin,
            fout;
    .
    .
    .
    Last edited by MrWizard; 04-30-2004 at 05:37 PM. Reason: Don't want smilies
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    26
    Like I said in the first part, I'm on an old compiler, putting the declaration outside main WILL NOT DO ANYTHING. I have 0 syntax errors. Its not reading when I use fsbin.read. Also my compiler already uses ios::binary.

  5. #5
    Caffienated jinx's Avatar
    Join Date
    Oct 2001
    Posts
    234
    Code:
    #include <fstream.h>
    #include <stdlib.h>
    
    struct UPGRADE
    {
    int TwoWeeksSalary;
    char Ebay;
    char FedEx
    };
    does this help?


    Weeel, itss aboot tieme wee goo back too Canada, eeehy boyss.

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    	 fsbin.read((char*)&worker.id, sizeof(int));
    	 cout << "\nThe data for employee " << idNum << "is: \n"
              << worker.id << "\t" << worker.firstname << "\t"
      		  << worker.lastname << "\t" << worker.salary << "\t"
    		  << (worker.salary / 12.0) << "\n";
    You're only reading in a single int(the id) and then expecting the other members of the employee struct to be valid.

    Possibly, you want to read in the entire structure?
    Code:
     fsbin.read((char*) &worker, sizeof(worker));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Tree - Reading From and Writing to a File
    By Ctank02 in forum C++ Programming
    Replies: 2
    Last Post: 03-15-2008, 09:22 PM
  2. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  3. Problem reading a delimited file into a binary tree
    By neolyn in forum C++ Programming
    Replies: 10
    Last Post: 12-09-2004, 07:51 PM
  4. Reading data from a binary file
    By John22 in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 02:00 PM
  5. reading from structs in a binary file
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 12-21-2001, 10:52 AM