程序从存储在自身中的变量读取而不是从二进制文件读取

时间:2014-12-05 03:52:55

标签: c++ fstream binaryfiles

我正在制作一个对我的编程课程非常重要的程序,并且我可以解决这个问题;当我尝试从打开程序后创建的二进制文件中读取时,即使目录中的文件已经失败,在我尝试擦除文件内容后,我仍然可以& #39;读'它们来自文件,即使我在资源管理器中检查它时该文件是空的。我已经确定,即使我使用的是BinaryFile.read,它也不是真正从文件中读取,而是从存储在程序本身中的变量中读取。如何让我的程序从实际文件中读取?

(请注意,这还不是一个完整的程序,因此注释部分和空函数。)

(另请注意,由于我班级的性质,我只能使用已经教过的内容(即fstream标题中的任何内容以及之前制作基本程序所需的大部分内容 - 他和#39; s让我在stdio.h中使用东西。)

//
//  main.cpp
//  Binary Program
//
//  Created by Henry Fowler on 11/19/14.
//  Copyright (c) 2014 Bergen Community College. All rights reserved.


#include <iostream>
#include <iomanip> 
#include <fstream>
#include <string>
#include <cstdlib> 
#include <math.h>
#include <stdio.h>
using namespace std;
struct Record
{
    char Name[20];
    char LastName[20]; 
    double Pay;
    int Clearance;
    int ID;
};
void CreateFile(fstream&); //Working
void CheckExist(fstream&); //Working
void Populate(fstream&,Record[],int&,int&); //Working
void Display(fstream&,Record[],int&,int&); //Working
void Append(fstream&,Record[],int&,int&); //Working
void DeleteFromFile(fstream&,fstream&,Record[],int&,int&); 
// void SearchInFile(fstream&,Record[],int&,int&);
// void ModifyRecord(fstream&,Record[],int&,int&);
//void SortFile();
void WipeFile(fstream&);
void DelFile(fstream&);




int main(int argc, const char * argv[])
{

    Record EmpRecords[20];
    char Binary[] = "BinaryFile.dat";
    char Binary2[] = "BinaryFileTemp.dat"; 
    int maxsize; //make sure to set i to max size so you can use it later for things like wiping the    file or deleting specific records
    fstream BinaryFile;
    fstream BinaryFile2; 
    string InputStr;
    // char Read;
    //int Choice = 0;
    int i = 0;
    int choice = 0;
    int switchchoice;
    CreateFile(BinaryFile); //working
    CheckExist(BinaryFile); //working
    BinaryFile.close();  
    while(choice==0)
    {

        cout << "Options: " << endl;
        cout << "End Program (0)" << endl;
        cout << "Input new records to file (1)" << endl;
        cout << "Display current contents of file (2)" << endl;
        cout << "Append a record at the end of the file (3)" << endl;
        cout << "Delete a record from the file (4)" << endl;
        cout << "Search for a record in the file (5)" << endl;
        cout << "Modify a certain record (6)" << endl;
        cout << "Sort file (unimplemented)" << endl;
        cout << "Wipe contents of file (8)" << endl;
        cout << "Please choose an option: ";
        cin >> switchchoice;

        switch(switchchoice)
        {
            case 0:
            {
                cout << "Exiting.";
                BinaryFile.close(); 
                system("PAUSE");
                return 0;
                break;
            }
            case 1:
            {
                Populate(BinaryFile, EmpRecords,i,maxsize); //working
                break;
            }
            case 2:
            { 
                Display(BinaryFile, EmpRecords,i,maxsize); //working i think
                break;
            }
            case 3:
            { 
                Append(BinaryFile, EmpRecords,i,maxsize); //working
                break;
            }
            case 4:
            {
                DeleteFromFile(BinaryFile,BinaryFile2,EmpRecords,i,maxsize); //!
                break;
            }
            case 5:
            {
           //     SearchInFile(BinaryFile, EmpRecords,i,maxsize); //!
                break;
            }
            case 6:
            {
          //      ModifyRecord(BinaryFile, EmpRecords,i,maxsize); //!
                break;
            }
            case 7:
            { 
                cout << "Error, file sorting is currently unimplemented. Please try again.";
                break;
            }
            case 8:
            {
                WipeFile(BinaryFile); 
                break;
            }
        }

    }
    system("PAUSE");
    return 0;
}


void CreateFile(fstream& BinaryFile)
{
   BinaryFile.open("BinaryFile.dat", ios::out | ios::binary);

}

void CheckExist(fstream &BinaryFile)
{

    if(BinaryFile.good())
    {
        cout << endl << "File does exist" << endl;
    }
    else
    {
        cout << "file named can not be found \n";
    system("PAUSE"); 

   }

}

void Populate(fstream &BinaryFile,Record EmpRecords[],int &i, int &maxsize)
{
    BinaryFile.open("BinaryFile.dat", ios::out | ios::binary);

    int choice = 0;
    while(choice==0)
{
cout << "Please input employee first name: ";
cin >> EmpRecords[i].Name; 
cout << "Please input employee last name: ";
cin >> EmpRecords[i].LastName; 
cout << "Please input Employee Pay: ";
cin >> EmpRecords[i].Pay; 
cout << "Please input Employee Clearance (1-10): ";
cin >> EmpRecords[i].Clearance;
cout << "Please input Employee ID (6 numbers, i.e. 122934): ";
cin >> EmpRecords[i].ID; 
cout << "Input another employee's information? (0) = yes, (1) = no: ";
cin >> choice;
BinaryFile.write((char *) (&EmpRecords[i]),sizeof(EmpRecords[i]));
i = i+1; 
}
maxsize = i; 
cout << "i is " << i << endl; 
cout << "maxsize is " << maxsize << endl; 
BinaryFile.close(); 

}

void Display(fstream &BinaryFile,Record EmpRecords[],int &i,int &maxsize)
{
     BinaryFile.open("BinaryFile.dat", ios::in | ios::binary | ios::app);
     int i2 = maxsize;
     i = 0;  
     while(i2>0)
     {
                BinaryFile.read((char *) (&EmpRecords[i]),sizeof(EmpRecords[i]));
                cout << i << endl; 
                cout << EmpRecords[i].Name << " " << EmpRecords[i].LastName << endl; 
                cout << "Pay: $" << EmpRecords[i].Pay << endl; 
                cout << "Clearance: " << EmpRecords[i].Clearance << endl; 
                cout << "Employee ID: " << EmpRecords[i].ID << endl; 
     BinaryFile.read((char *) (&EmpRecords[i]),sizeof(EmpRecords[i]));
     cout << endl; 
     i2 = i2-1; 
     i = i+1;
     }

     BinaryFile.close(); 
}

void Append(fstream &BinaryFile,Record EmpRecords[],int &i,int &maxsize)
{
     BinaryFile.open("BinaryFile.dat", ios::out|ios::binary|ios::ate|ios::app);    

    cout << "Please input employee first name: ";
    cin >> EmpRecords[maxsize].Name;
    cout << "Please input employee last name: "; 
    cin >> EmpRecords[maxsize].LastName;  
    cout << "Please input Employee Pay: ";
    cin >> EmpRecords[maxsize].Pay; 
    cout << "Please input Employee Clearance (1-10): ";
    cin >> EmpRecords[maxsize].Clearance;
    cout << "Please input Employee ID (6 numbers, i.e. 122934): ";
    cin >> EmpRecords[maxsize].ID; 
    cout << "Input another employee's information? (0) = yes, (1) = no: ";
    BinaryFile.write((char *) (&EmpRecords[i]),sizeof(EmpRecords[i]));
    maxsize = maxsize+1; 
    cout << "maxsize is " << maxsize << endl;  
    BinaryFile.close(); 

    }

void DeleteFromFile(fstream &BinaryFile,fstream &BinaryFile2, Record EmpRecords[],int &i,int     &maxsize)
{
    BinaryFile.open("BinaryFile.dat", ios::out|ios::binary|ios::app);
    BinaryFile2.open("BinaryFileTemp.dat", ios::out|ios::binary|ios::app); 
    int Choice; 
    cout << "Would you like to delete a file by name or by employee number?" << endl;
    cout << "Name (1)" << endl; 
    cout << "Number (2)" << endl; 
    cout << "Choice: "; 
    cin >> Choice; 
    int i2 = maxsize; 
    if(Choice==1)
    {
                 cout << "Please input employee first name: "; 
                 // cin >> firstname; 
                 cout << "Please input employee last name: ";
                 // cin >> lastname; 
                 cout << "Searching..."; 
                 int i2 = maxsize;
                 i = 0;  
                 while(i2>0)
                 {
                            BinaryFile.read((char *) (&EmpRecords[i]),sizeof(EmpRecords[i]));
                            cout << i << endl; 
                            BinaryFile.read((char *) (&EmpRecords[i]),sizeof(EmpRecords[i]));
                         //   if(EmpRecords[i].Name == firstname)
                         //   {
                         //                         cout << "Found first name." << endl; 
                        //                          if (EmpRecords[i].LastName == lastname)
                         //                         {
                     //                           cout << "Found last name." << endl;                          
                     ///                         }
                    //    }
                    //    else
                    //    {
                    //        cout << "Could not find name."; 
                   // //     BinaryFile2.write((char *) (&EmpRecords[i]),sizeof(EmpRecords[i]));
                   //      }                                            

                        cout << endl; 
                        i2 = i2-1; 
                        i = i+1;
             }



    }       
            BinaryFile.close();
            if( remove( "BinaryFile.dat" ) != 0 )
            cout << endl << "Error deleting file" << endl;
            else
            {
            cout << "File successfully deleted" << endl << endl;
            }
            int result; 
            char oldname[]="BinaryFileTemp.dat";
            char newname[]="BinaryFile.dat"; 
            result = rename(oldname,newname); 
            if(result == 0)
            cout << "DEBUG: Success" << endl; 
            else
            cout << "DEBUG: Failure" << endl; 


}


void WipeFile(fstream &BinaryFile)
{
                int sure;
                cout << "There is no undoing this action." << endl;
                cout << "Continue (1)" << endl;
                cout << "Cancel (2)" << endl;
                cout << "Wipe file? ";
                cin >> sure;
                if(sure == 1)
                {
                    cout << "Wiping file.";
                    BinaryFile.open("BinaryFile.dat", ios::out | ios::binary | ios::trunc); 
                    BinaryFile.close(); 
                }
                else
                {
                    cout << "Canceling.";
                }
}

void DelFile(fstream &BinaryFile)
{    
    BinaryFile.close();
    if( remove( "BinaryFile.dat" ) != 0 )
    cout << endl << "Error deleting file" << endl;
    else
    {
        cout << "File successfully deleted" << endl << endl;
    }
}

1 个答案:

答案 0 :(得分:0)

问题似乎是,即使您正在擦除文件内容,也不会清除Record EmpRecords[20];中存储的数据int maxsize值。

void WipeFile(fstream &BinaryFile)函数中您可以执行的操作很少:为了简单起见,我们只需将maxsize重置为0:

  1. maxsize变量作为WipeFile()的引用,与传递Populate()的方式相同
  2. 更新maxsize = 0,表示删除文件内容时删除所有记录。
  3. memset的内容最好同样EmpRecords

    目前,我只是修改了您的代码,以便在maxsize中将WipeFile()重置为0,并且确实有效。

    void WipeFile(fstream &BinaryFile, int &maxsize)
    {
        int sure;
        cout << "There is no undoing this action." << endl;
        cout << "Continue (1)" << endl;
        cout << "Cancel (2)" << endl;
        cout << "Wipe file? ";
        cin >> sure;
        if(sure == 1)
        {
            cout << "Wiping file.";
            BinaryFile.open("BinaryFile.dat", ios::out | ios::binary | ios::trunc); 
            BinaryFile.close(); 
            maxsize = 0;
        }
        else
        {
            cout << "Cancelling.";
        }
    }