1

尝试从结构写入二进制文件。不幸的是,它写出了过多的空格,因为我的字符串都是 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
4

3 回答 3

0

这可能是一个不好的方法。就像宇宙先生在评论中所说的那样,这些数组中包含所有没有数据的额外空白。我会将案例 2 代码更改为更像以下内容:

//#include <iomanip> for the stream formatting used when printing to the file
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())
        {
            //Read file
            char eol[10];
            while(BinaryOpen.good())
            {
                BinaryOpen >> favoriteEmployees[numberOfEmployees].firstName;
                BinaryOpen >> favoriteEmployees[numberOfEmployees].lastName;
                BinaryOpen >> favoriteEmployees[numberOfEmployees].hourlyWage;
                BinaryOpen >> favoriteEmployees[numberOfEmployees].hoursWorked;
                BinaryOpen >> eol;

                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())
            {
                for (int i = 0; i < numberOfEmployees; i++)
                {
                    BinaryWrite << favoriteEmployees[i].firstName << " ";
                    BinaryWrite << favoriteEmployees[i].lastName << " ";
                    BinaryWrite << setiosflags(std::ios::fixed) << std::setprecision(2) << favoriteEmployees[i].hourlyWage << " ";
                    BinaryWrite << setiosflags(std::ios::fixed) << std::setprecision(2) << favoriteEmployees[i].hoursWorked << std::endl;
                }

                //Close file
                BinaryWrite.close();
            }
            else
                std::cout << "\nWrite file did not open! " << std::endl;
        }
        else
            std::cout << "\nFile did not open! " << std::endl;
    }
    break;
于 2013-03-01T02:32:19.027 回答
0

正如我上面的评论。我已经准备了一个简单的示例,它将让您了解如何执行此操作。

void SaveString(fstream& file_stream, string output)
{
    // Note that any good string class will have an implicit conversion to char*
    if (output.size() == 0)
        return;

    file_stream << (unsigned char)output.size();

    for (unsigned char i=0; i<output.size(); i++)
        file_stream << output[i];

    // DONE (in theory)
}

然后,您需要做的就是在字符串的开头获取 1 个无符号字符,然后在 for 循环中使用该数字来获取字符并将它们添加到您加载的字符串中。

于 2013-03-01T02:35:14.107 回答
0

这就是它打印垃圾的原因:

BinaryWrite.write(reinterpret_cast <char *>(favoriteEmployees),
    sizeof(Employee) * numberOfEmployees);

这些可能不是您看到的空格,而是 NULL 字符或其他不可打印的字符。

您需要做的是编写文件字段:

BinaryWrite << favoriteEmployees[i].firstName << " ";
BinaryWrite << favoriteEmployees[i].lastName << " ";
BinaryWrite << std::setprecision(2) << favoriteEmployees[i].hourlyWage << " ";
BinaryWrite << std::setprecision(2) << favoriteEmployees[i].hoursWorked << std::endl;

调用这些二进制文件可能有点误导,因为它们实际上只包含文本。一个实际的二进制文件将包含固定长度、以长度为前缀或以零结尾的字符串,以及数字的二进制表示(您在原始代码中无意中这样做了)。

于 2013-03-01T02:39:13.280 回答