Trying to write to file...

This is a discussion on Trying to write to file... within the C++ Programming forums, part of the General Programming Boards category; This thing will not write to the file, please help! Code: /* Use data from keyboard to create a binary ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    4

    Unhappy Trying to write to file...

    This thing will not write to the file, please help!

    Code:
    /* Use data from keyboard to create a
       binary file of fixed length records */
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    struct info
       {
           char inv_num [16];
           char prod_name [21];
           char prod_code [4];
           int  oh_qty;
           int  bo_qty;
           int  oo_qty;
           float inv_eval;
           float avg_cost;
           float sell_price;
       };
     
    class inventory
    
    {
     protected:
     public:
            ofstream file;
     
    inventory()
     {
            ofstream file("inventory.dat", ios::out | ios::app | ios::binary);
     }
     ~inventory()
     {
            file.close();
     }
    
    void write_data(info &i)
    {
            file.write((char*) &i,sizeof(i));
    }
     
    };
     
    int main ()
    {
            inventory go;
            info x;
     
            cout << endl << "Inventory Number: ";
            cin.getline (x.inv_num, 16);
            go.write_data(x);
            return 0;
    }
    Thanks!

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Your constructor declares a new variable called file that goes out of scope when the constructor function ends. You want to construct the member variable with that information. To pass arguments to a constructor for a member variable use an initialization list:
    Code:
    inventory() : file("inventory.dat", ios::out | ios::app | ios::binary)
    {
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM

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