尝试从结构写入二进制文件。不幸的是,它写出了过多的空格,因为我的字符串都是 20 个字符长。我会使用指针,但我的教科书明确指出不要使用指针(即记住:写入二进制文件时仅使用固定大小的数据成员。不要使用指针或包含指针作为数据成员的类)。
更糟糕的是,我试图打印的数字(即 8 和 40)只是显示为“A”和“B”。
输出看起来像这样
Employee.dat(我正在写入的二进制文件)
Pauline Nordin A B
但是,我希望它看起来像这样。
Pauline Nordin 8.00 40.00
这是我的代码:
摘自protocol.cpp
//Open file
std::ifstream BinaryOpen("employee.txt", std::ios::in /*| std::ios::out*/ | std::ios::binary);
//Check if file is open
if(BinaryOpen.is_open())
{
//Priming read
BinaryOpen >> favoriteEmployees[numberOfEmployees].firstName;
BinaryOpen >> favoriteEmployees[numberOfEmployees].lastName;
BinaryOpen >> favoriteEmployees[numberOfEmployees].hourlyWage;
BinaryOpen >> favoriteEmployees[numberOfEmployees].hoursWorked;
numberOfEmployees++;
//Read file
while(!BinaryOpen.eof())
{
BinaryOpen >> favoriteEmployees[numberOfEmployees].firstName;
BinaryOpen >> favoriteEmployees[numberOfEmployees].lastName;
BinaryOpen >> favoriteEmployees[numberOfEmployees].hourlyWage;
BinaryOpen >> favoriteEmployees[numberOfEmployees].hoursWorked;
numberOfEmployees++;
//Close file
BinaryOpen.close();
}
//Write to binary file
std::ofstream BinaryWrite("employee.dat", std::ios::out | std::ios::binary);
//Check if file opened
if(BinaryWrite.is_open())
{
BinaryWrite.write(reinterpret_cast <char *>(favoriteEmployees),
sizeof(Employee) * numberOfEmployees);
//Close file
BinaryWrite.close();
}
else
std::cout << "\nWrite file did not open! " << std::endl;
}
else
std::cout << "\nFile did not open! " << std::endl;
}
以下是我的程序的所有文件:
employee.txt(即我正在读取的文件)
Pauline Nordin 8.00 40.00
主文件
#include <iostream>
#include "protocol.h"
#include "employee.h"
int main()
{
int menuChoice = 0;
int numberOfEmployees = 0;
//Create array of employees
Employee favoriteEmployees[NUMBER_OF_EMPLOYEES];
//To prevent garbage being printed out
for(int i = 0; i < BUFFER_LENGTH; i++)
{
favoriteEmployees[0].firstName[i] = 0;
favoriteEmployees[0].lastName[i] = 0;
favoriteEmployees[0].hourlyWage = 0;
favoriteEmployees[0].hoursWorked = 0;
}
PrintMenu();
GetMenuChoice(menuChoice);
ExecuteMenuChoice(menuChoice, favoriteEmployees, numberOfEmployees);
return 0;
}
协议.h
#ifndef PROTOCOL_H
#define PROTOCOL_H
#include "employee.h"
const int NUMBER_OF_EMPLOYEES = 10;
//Function declarations
void PrintMenu();
void GetMenuChoice(int &menuChoice);
void ExecuteMenuChoice(int menuChoice, Employee favoriteEmployees[], int &numberOfEmployees);
#endif
协议.cpp
#include <fstream>
#include <iostream>
#include "employee.h"
#include "protocol.h"
//Function definitions
void PrintMenu()
{
std::cout << "\n\nChapter 17 -- Learn By Doings " << std::endl;
std::cout << "\n1. Learn By Doing 17.2 " << std::endl;
std::cout << "2. Learn By Doing 17.3 " << std::endl;
std::cout << "3. Learn By Doing 17.4 " << std::endl;
std::cout << "4. Exit " << std::endl;
std::cout << ' ' << std::endl;
}
void GetMenuChoice(int &menuChoice)
{
std::cin >> menuChoice;
}
void ExecuteMenuChoice(int menuChoice, Employee favoriteEmployees[], int &numberOfEmployees)
{
switch(menuChoice)
{
case 1:
{
//Open file in append mode
std::ifstream BinaryOpen("name.txt", std::ios::app | std::ios::binary);
//Open file in write mode
/*std::ofstream BinaryOpen("name.txt", std::ios::out | std::ios::binary);*/
//Check if file is open
if(BinaryOpen.is_open())
{
//Perform appropriate file operatings
std::cout << "\nFile opened! " << std::endl;
//Close file
BinaryOpen.close();
}
//Else
else
std::cout << "\nFile did not open! " << std::endl;
}
break;
case 2:
{
//Open file
std::ifstream BinaryOpen("employee.txt", std::ios::in /*| std::ios::out*/ | std::ios::binary);
//Check if file is open
if(BinaryOpen.is_open())
{
//Priming read
BinaryOpen >> favoriteEmployees[numberOfEmployees].firstName;
BinaryOpen >> favoriteEmployees[numberOfEmployees].lastName;
BinaryOpen >> favoriteEmployees[numberOfEmployees].hourlyWage;
BinaryOpen >> favoriteEmployees[numberOfEmployees].hoursWorked;
numberOfEmployees++;
//Read file
while(!BinaryOpen.eof())
{
BinaryOpen >> favoriteEmployees[numberOfEmployees].firstName;
BinaryOpen >> favoriteEmployees[numberOfEmployees].lastName;
BinaryOpen >> favoriteEmployees[numberOfEmployees].hourlyWage;
BinaryOpen >> favoriteEmployees[numberOfEmployees].hoursWorked;
numberOfEmployees++;
//Close file
BinaryOpen.close();
}
//Write to binary file
std::ofstream BinaryWrite("employee.dat", std::ios::out | std::ios::binary);
//Check if file opened
if(BinaryWrite.is_open())
{
BinaryWrite.write(reinterpret_cast <char *>(favoriteEmployees),
sizeof(Employee) * numberOfEmployees);
//Close file
BinaryWrite.close();
}
else
std::cout << "\nWrite file did not open! " << std::endl;
}
else
std::cout << "\nFile did not open! " << std::endl;
}
break;
case 3:
break;
case 4:
break;
default:
std::cout << "\nInvalid input. Please enter an integer from 1 to 4. " << std::endl;
}
}
**employee.h**
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
const int BUFFER_LENGTH = 20;
struct Employee
{
char firstName[BUFFER_LENGTH];
char lastName[BUFFER_LENGTH];
float hourlyWage;
float hoursWorked;
};
#endif