开发者

binary file created in c++ but content deleted after running

Backstory: I'm doing a C++ school project. I need write a program that, among other things, creates a binary file if it doesn't already exist, allows the user to modify it, and saves the file so that it can be opened again and read or modified. I have written the main program separately, but I haven't been able to get the file input and output to work appropriately, so I have been experimenting with modifying some sample code from my textbook CD. Most of the textbook samples come with an existing .dat file that is supposed to be loaded, or only create, only write, or only read. However, our professor wants us to turn in the .cpp file without a .dat file. The program is supposed to generate the .dat file and allow for reading and writing. So, I don't have good examples to work from.

Main point: Why does this program seem to create a file, write to it and read from it, and then when it closes, I go to the directory where the .dat file is stored, and the file is blank (says 0 bytes)? How can I get the content to stay in the file when I close? Is it even being properly created in the first place?

(By the way, I know that after the prompt to enter data, it displays garbage. I just set that up to see if there was content in the record before data input.)

// This program allows the user to edit a specific record.
#include <iostream>
#include <fstream>
using namespace std;

const int DESC_SIZE = 31;  // Description size

// Declaration of InventoryItem structure
struct InventoryItem
{
   char desc[DESC_SIZE];
   int qty;
   double price;
};

int main()
{
   InventoryItem record;  // To hold an inventory record
   long recNum;           // To hold a record number
   long numBytes;

   fstream inventory;

   // Open the file in binary mode for input and output
    inventory.open("Inventory.dat",
                     /*ios::in | */ios::out | ios::binary);

   //file.open("Inventory.dat", ios::in | ios::out | ios::binary);
   if (!inventory.is_open())
   {
      cout << "Creating ...\n";
      // Create the file.
      inventory.open("Inventory.dat", ios::out);
      // Close the file.
      inventory.close();
      // Reopen the file for input and output.
      inventory.open("Inventory.dat", ios::in | ios::out | ios::binary);
   }
   inventory.seekg(0L, ios::end);
   numBytes = inventory.tellg();
   cout << "After opening: The file has " << numBytes << " bytes." << endl;

   // Get the record number of the desired record.
   cout << "Which record do you want to edit? ";
   cin >> recNum;

   // Move to the record and read it.
   inventory.seekg(recNum * sizeof(record), ios::beg);
   inventory.read(reinterpret_cast<char *>(&record),
                 sizeof(record));

   // Display the record contents.
   cout << "Description: ";
   cout << record.desc << endl;
   cout << "Quantity: ";
   cout << record.qty << endl;
   cout << "Price: ";
   cout << record.price << endl;

   // Get the new record data.
   cout << "Enter the new data:\n";
   cout << "Description: ";
   cin.ignore();
   cin.getline(record.desc, DESC_SIZE);
   cout << "Quantity: ";
   cin >> record.qty;
   cout << "Price: ";
   cin >> record.price;

   // Move back to the beginning of this record's position.
   inventory.seekp(recNum * sizeof(record), ios::beg);

   // Write the new record over the current record.
   inventory.write(reinterpret_cast<char *>(&record),
                  sizeof(record));


   inventory.seekg(0L, ios::end);
   numBytes = inventory.tellg();
   cout << "The file has " <&l开发者_JAVA技巧t; numBytes << " bytes.";

 // Move to the record and read it.
   inventory.seekg(recNum * sizeof(record), ios::beg);
   inventory.read(reinterpret_cast<char *>(&record),
                 sizeof(record));

   // Display the record contents.
   cout << "Description: ";
   cout << record.desc << endl;
   cout << "Quantity: ";
   cout << record.qty << endl;
   cout << "Price: ";
   cout << record.price << endl;

   inventory.seekg(0L, ios::end);
   numBytes = inventory.tellg();
   cout << "The file has " << numBytes << " bytes.";

   // Close the file.
   inventory.close();


    //Try opening the file again.
    inventory.open("Inventory.dat",
                     ios::in | /*ios::out |*/ ios::binary);

 // Move to the record and read it.
   inventory.seekg(recNum * sizeof(record), ios::beg);
   inventory.read(reinterpret_cast<char *>(&record),
                 sizeof(record));

   // Display the record contents.
   cout << "Description: ";
   cout << record.desc << endl;
   cout << "Quantity: ";
   cout << record.qty << endl;
   cout << "Price: ";
   cout << record.price << endl;

   inventory.seekg(0L, ios::end);
   numBytes = inventory.tellg();
   cout << "The file has " << numBytes << " bytes.";


   return 0;
}


I'm not sure what the policy on homework on this site is; but I feel it is appropriate to only give you hints and not an answer.

Observation: you do not check any of your calls to the stream, so after say inventory.write() you do not check that the write was successful. You may do this via any of the state functions, ie: inventory.good(). Detecting where file access fails is going to help you identify your problem.

What happens if there is no file (so it is created) and I enter record number 23? That is, if the file size is 0 what do you think will happen with the call inventory.seekg(23 * sizeof (InventoryItem)).

If this seems cryptic: think about the difference between making this call on a pre-existing file, and a newly created file. I believe once you recognize the significant difference, the solution will be clear.

If you are struggling put in error checking code as mentioned above, this will help direct your investigation.

Good luck.


Maybe you need to flush the fstream (inventory)?


Maybe you should call

inventory.close(); //?

to commit the change

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜