2012-02-04 84 views
0

嗨我正在製作一個簡單的類程序,用於使用結構將客戶記錄存儲在二進制文件中。
問題與「顯示全部」功能 到目前爲止,我可以將記錄添加到二進制文件,但是當我嘗試使用我的DisplayAll函數列出所有文件項時,它只是向我吐出垃圾。二進制文件中的結構輸出返回垃圾

二進制文件的簡單讀取功能工作正常,所以我可以一次讀取一個文件。 我想知道如果問題是與我的代碼中的邏輯。

我調試,但最終在fstream內置代碼。

*/ Program #2 
    This program will use structs to store data about 
    accounts */ 


#include <stdio.h> 
#include <stdlib.h> 
#include <iostream> 
#include <fstream> 
#include <cstring> 

using namespace std; 

const int SIZE = 16; 

**struct Cust 
{ 
    char name[SIZE]; 
    char phone[SIZE]; 
    float accountBal; 
};** 

// Function Prototypes 

// Function to enter new records into file. 

// Function to display menu 
void menu(); 
void addCust(fstream&); 
void displayAll(fstream&); 

int main() 
{ 
    char choice [4]; 
    fstream custBinFile; // Filestream for I/O to Binary File 

    // Opening Binary File for Both input and output. 
    custBinFile.open("custBinFile.dat", ios::out|ios::in|ios::binary); 

    cout<< "Enter any choice from the above Menu List:"<<endl; 
    do 
    { 
     menu(); 
     cin >> choice; 
     // If to handle adding a record to file 
     **if((strcmp(choice, "A") == 0) || (strcmp(choice, "a") == 0)) 
     { 
      addCust(custBinFile); 
     }** 
     // Displaying one record from File 
     else if((strcmp(choice, "F") == 0)|| (strcmp(choice, "f") == 0)) 
     { 
      cout << "Find and Display record \n" << endl; 
     } 
     // Handles deleting customer record 
     else if((strcmp(choice, "D") == 0)|| (strcmp(choice, "d") == 0)) 
     { 
      cout << "Delete Customer Record \n" << endl; 
     } 
     // Find and Change record 
     else if((strcmp(choice, "C") == 0)|| (strcmp(choice, "c") == 0)) 
     { 
      cout << "Find and Change Record \n" << endl; 
     } 
     // Displays all contents sorted in order 
     **else if((strcmp(choice, "L") == 0)|| (strcmp(choice, "l") == 0)) 
     { 
      displayAll(custBinFile); 
     }** 
     // handles Bad input: Input Validation 
     else if((strcmp(choice, "E") != 0)|| (strcmp(choice, "e") != 0)) 
      cout << "Bad input, you twit!! Change that" << endl; 

    } while((strcmp(choice, "E") != 0) && (strcmp(choice, "e") != 0)); 

    cout << "Exiting now "; 


    custBinFile.close(); 
    return 0; 
} 

void menu() 
{ 
    cout<< "Main Menu"<<endl; 
    cout<< " \n"; 
    cout<< "A - Add Customer Record \n"; 
    cout<< "F - Find and Display Record\n"; 
    cout<< "D - Delete Customer Record\n"; 
    cout<< "C - Change Customer Record\n"; 
    cout<< "L - List All Records\n"; 
    cout<< "E - Exit\n"; 
    cout<< " \n"; 
    cout<< "Enter letter corresponding to your choice:"<<endl; 
} 

**void addCust(fstream& custBinFile) 
{ 
    // Initialise Variables 
    Cust custType; 
    // User inputs record Info. 
    cout << "Enter the customer's name: "; 
    cin >> custType.name; 
    cout << "\n Phone Number: "; 
    cin >> custType.phone; 
    cout << "\n Account Balance: "; 
    cin >> custType.accountBal; 

    // If empty record found in File, search it and overwrite it with new record 
    // Otherwise, add record to Binary File. 
    // Open record to Binary File. 
    custBinFile.write(reinterpret_cast<char *> (&custType), sizeof(custType)); 
}** 

**void displayAll(fstream& custBinFile) 
{ 
    // Displaying all Structs in File. 
    Cust custType; 
    while(!custBinFile.eof()) 
    { 
     custBinFile.read(reinterpret_cast<char *> (&custType), sizeof(custType)); 
     cout << "Customer Name: " << custType.name << endl; 
     cout << "Customer Phone Number: " << custType.phone << endl; 
     cout << "Customer Account Balance: " << custType.accountBal << endl; 
    } 
}** 

回答

0

看起來,如果你foget在文件中了追加一個新的記錄之前,移動位置到底喜歡(或試圖從文件末尾讀)。 upd:順便說一下,由於咬合順序和文字對齊問題,以這種方式編寫並不是一個好主意。

+0

是的,我使用seekg和seekp函數修復了它。現在編正在研究最後兩個功能。 – onelovelxg 2012-02-08 05:28:16