二进制文件中的Struct输出返回垃圾

时间:2012-02-04 09:40:25

标签: c++ struct binaryfiles eof

您好我正在制作一个关于使用结构将客户记录存储在二进制文件中的简单类程序 问题在于“全部显示”功能 到目前为止,我可以将记录添加到二进制文件,但是当我尝试使用我的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;
    }
}**

1 个答案:

答案 0 :(得分:0)

看起来如果您想要在添加新记录之前将文档中的位置移动到最后(或者您尝试从文件的末尾读取)。 upd:顺便说一句,由于咬合顺序和单词对齐问题,以这种方式编写不是一个好主意。