2014-12-05 54 views
-1

我正在研究一個對我的編程類非常重要的程序,並且有一些我不太清楚的地方;當我嘗試從打開程序後創建的二進制文件中讀取數據時,即使文件位於目錄中,但在嘗試擦除文件內容後,仍會失敗,因此我仍然可以從文件中「讀取」它們即使我在資源管理器中檢查它時該文件是空的。我已經確定,即使我使用BinaryFile.read,它並不真正從文件中讀取數據,而是從存儲在程序本身中的變量中讀取數據。我如何讓我的程序從實際文件中讀取?程序從存儲在變量本身而不是從二進制文件中讀取的變量

(請注意,這還不是一個完整的程序,因此,註釋的部分和空函數)。

(另請注意的是,由於我的課的性質,我只允許使用什麼一直被教導已經(即,在fstream頭文件什麼,最事情之前會進行必要作出一個基本的程序 - 他讓我使用的東西在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; 
    } 
} 
+0

最可能的解釋是'BinaryFile.dat'沒有存儲在你認爲它的地方,你的程序會將該文件保存在當前工作目錄中。 – user657267 2014-12-05 04:00:31

+0

您確實需要更多錯誤檢查,以便您知道文件是否在您試圖訪問的任何位置打開並且數據已成功讀取/寫入。 – 2014-12-05 04:52:31

+0

我可以驗證該文件是否存在,當我使用它,它創建等等。問題是,當我擦拭文件,我檢查它在資源管理器和文件是空的 - 當我選擇要顯示結果的程序,它應該是空白的。相反,它會顯示我輸入的內容,就好像文件中仍然有數據一樣。 – Codetographer 2014-12-05 19:23:38

回答

0

這裏的問題似乎儘管您正在擦除文件內容,但您並未清除已存儲的數據Record EmpRecords[20];int maxsize的值。

很少有東西可以在裏面void WipeFile(fstream &BinaryFile)功能做的事:爲了簡單起見,我們只重maxsize以0:

  1. 傳遞maxsize變量作爲參考WipeFile(),要傳遞的Populate()以同樣的方式
  2. 更新maxsize = 0,表示刪除文件內容時刪除所有記錄。

memsetEmpRecords的內容也相似。

現在,我剛剛修改了您的代碼,將maxsize重設爲0,WipeFile()並且它工作正常。

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."; 
    } 
} 
+0

你說得對@donyjo,代碼對我來說工作順利。 – 2015-01-14 16:13:45

+0

不要把你的代碼放在代碼片段框中,它只能用於預覽HTML代碼。我在這篇文章中做了編輯。 – 2015-01-14 16:59:14