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

发布于 2025-01-02 09:30:56 字数 4020 浏览 0 评论 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;
    }
}**

Hi Am making a simple class program about storing Customer records in a binary file using structs.
Problem is with "Display All" function
So far i can add records to binary file but when i try to list all file items using my DisplayAll function, it just spits out junk at me.

the simple read function for binary files works fine so i can read one file at a time.
Am wondering if the problem is with my logic in my code.

I debugged but ended up in fstream inbuilt code.

    */ 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;
    }
}**

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迟月 2025-01-09 09:30:58

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

It looks like if you foget to shift position in a file up to the end before appending a new record (or you try to read from the end of a file).
upd: by the way, it is not a good idea to write in such a way due to bite-order and words-aligning issues.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文