二进制文件中结构的输出返回垃圾
Output from Struct in Binary File returns junk
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:顺便说一句,由于咬合顺序和单词对齐问题,用这种方式写作不是一个好主意。
相关文章:
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 返回 str vs. str.substr(0,str.size()) 在 leetcode 中给了我不同的输出
- C++输出参数与返回值
- cpp 返回具有相同输入的不同输出
- 显示C++输出而不返回值
- 简单函数并不总是返回预期的输出
- 输出返回编号。等于输入字符串的单词的字符串数
- 如何在C++中执行命令并获取命令的返回代码标准输出和标准
- 我的动态链接队列在同一输出流中调用时不正确地输出三个返回函数
- 为什么 boost::locale::to_title 不返回预期的输出?
- 在函数中连接两个字符,并在 C++ 中返回输出
- 为什么我的数独程序不返回输出?
- 如何将变量传递给父,但也返回输出值?(C )
- C++字符返回输出符号
- MSSQL服务器存储过程在从c++调用时不返回输出参数
- c++中的字符串返回类型不返回输出
- 从文本文件返回输出的函数
- 在vc++中使用CreateProcess和CreatePipe来执行进程并以字符串形式返回输出
- 从bash脚本返回输出到调用c++函数
- 如何更改用户输入字符,例如。"abcDEF"并返回输出"222333"