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

Output from Struct in Binary File returns junk

本文关键字:返回 输出 结构 二进制文件      更新时间:2023-10-16

Hi-Am制作了一个关于使用structs将Customer记录存储在二进制文件中的简单类程序
"全部显示"功能存在问题到目前为止,我可以将记录添加到二进制文件中,但当我尝试使用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 Recordn";
    cout<< "D - Delete Customer Recordn";
    cout<< "C - Change Customer Recordn";
    cout<< "L - List All Recordsn";
    cout<< "E - Exitn";
    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;
    }
}**

这看起来像是在附加新记录之前将文件中的位置向上移动到末尾(或者尝试从文件末尾读取)。upd:顺便说一句,由于咬合顺序和单词对齐问题,用这种方式写作不是一个好主意。