保存并退出并返回加载后,fstream无法加载我保存在程序中的文件,C ++,Xcode

时间:2018-11-30 21:27:01

标签: c++ xcode

在我的作业中,我正在将数据写入要保存(功能保存)的文件“ database.txt”(功能Enter),然后能够在退出并返回程序后加载该文件(功能重新打开程序)。每次我想为显示功能按5时,即使单击“加载”后,我仍被告知数据库为空。不知道我是否缺少任何东西,请帮忙。

#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;

// the format of each of the elements in the database
struct OpAmps {
char Name[20]; // the name of the op-amp (e.g. "741")
unsigned int PinCount; // the number of pins in the package
double SlewRate; // the slew rate in volts per microsecond
};

// the length of the fixed array to be used for database - must be at least one
// and no greater the maximum value allowed in an unsigned long (see the file
// limits.h).
#define DATABASE_MAX 10

// file used for the database
#define DATABASE_FILENAME "database.txt"

// function prototypes

void Enter ( OpAmps&, unsigned long&);
void Save ( OpAmps*, unsigned long);
void Load (OpAmps*,  unsigned long&);
void Sort (OpAmps*, unsigned long);
void Display ( OpAmps*, unsigned long); 

// Control the entering, saving, loading, sorting and displaying of elements in the
// database.
// Arguments: None
// Returns: 0 on completion
int main()
{
OpAmps OpAmp[DATABASE_MAX];   // the database
unsigned long database_length = 0;  // the number of elements in the   database
char UserInput;

// loop until the user wishes to exit
while (1) {

    // show the menu of options
    cout << endl;
    cout << "Op-amp database menu" << endl;
    cout << "--------------------" << endl;
    cout << "1. Enter a new op-amp into the database" << endl;
    cout << "2. Save the database to disk" << endl;
    cout << "3. Load the database from disk" << endl;
    cout << "4. Sort the database" << endl;
    cout << "5. Display the database" << endl;
    cout << "6. Exit from the program" << endl << endl;

    // get the user's choice
    cout << "Enter your option: ";
    cin >> UserInput;
    cout << endl;

    // act on the user's input
    switch(UserInput) {
        case '1':
            Enter(OpAmp[database_length], database_length);
            break;

        case '2':
            Save(OpAmp, database_length);
            break;

        case '3':
            Load(OpAmp, database_length);
            break;

        case '4':
            Sort(OpAmp, database_length);
            break;

        case '5':
            Display(OpAmp, database_length);
            break;

        case '6':
            return 0;

        default:
            cout << "Invalid entry" << endl << endl;
            break;
    }
    }
    }


// Allow the user to enter a new element into the database. Note that the data is
// simply added to the end the database (if not full) and no sorting is carried
// out.
// Arguments:
//   (1) the element in the database to be entered
//   (2) the position of the element in the database
// Returns: void
void Enter ( OpAmps& OpAmp, unsigned long& length)
{
// if the database is full, inform the user
if (length >= DATABASE_MAX )
{
    cout << "Database is full." << endl;
}

// if the database is not full, get the data from the user and alter the database
// length
else
{
    cout << "Enter OpAmp Name: ";
    cin >> OpAmp.Name;
    cout << "Enter Pin Count: ";
    cin >> OpAmp.PinCount;
    cout << "Enter Slew Rate: ";
    cin >> OpAmp.SlewRate;

    length++;
}
}


// Save the database to the file specified by DATABASE_FILENAME. If the file
// exists it is simply overwritten without asking the user.
// Arguments:
//   (1) the database
//   (2) the length of the database
// Returns: void
void Save (OpAmps* DB, unsigned long length)

{
fstream output_file;  // file stream for output

// open the file
output_file.open("database.txt", fstream::out);

if (!output_file.good()) {cout <<"Error"; exit(1);}

// write length information to file
output_file << length << endl;

// write data to file
for (int i = 0; i < length; i++)
{
    output_file << DB[i].Name << DB[i].PinCount << DB[i].SlewRate;

}

// close the file
output_file.close();
}


// Load the database from the file specified by DATABASE_FILENAME. If the file
// exists it simply overwrites the data currently in memory without asking
// the user.
// Arguments:
//   (1) the database
//   (2) the length of the database
// Returns: void
void Load (OpAmps* DB, unsigned long& length)
{
fstream input_file;  // file stream for input

// open the file
input_file.open("database.txt", fstream::in);

if (!input_file.good()) {cout <<"Error"; exit(1);}

// load database length information from file
for (int i = 0; i < length; i++)
{
    input_file >> length;
}

// load data from file
for (int i = 0; i < length; i++)
{
    input_file >> DB[i].Name >> DB[i].PinCount >> DB[i].SlewRate;
}

// close the file
input_file.close();
}

// Display all of the messages in the database.
// Arguments:
//   (1) the database
//   (2) the length of the database
// Returns: void
void Display ( OpAmps* DB, unsigned long length)
{
    cout << "Display running " << endl << endl;
    // if the database is empty, inform the user
    if (length == 0 )
    {
        cout << "Database is empty." << endl;
    }

    // if the database is not empty, display all the elements in the     database
    else
    {
        for (int i = 0; i < length; i++)
        {
            cout << "Name: "<< DB[i].Name << endl << "Pin Count: "     <<DB[i].PinCount << endl << "Slew Rate: " << DB[i].SlewRate << endl <<     endl;
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

main中,您将database_length初始化为零。按下5时,此零值将不变地传递到Display。然后Display显示该消息,因为if语句检查此参数是否为零。

仅当循环迭代在调用此database_length函数之前调用以Display作为参考的函数之一时,它的值才能不同。

但是Load,如果传递了零值的database_length,则实际上永远不会改变它,因为length的唯一变化发生在运行length迭代的循环中。

Enter可以递增length。但是我假设您在尝试调用Display之前没有调用它(即使它与database.txt文件中的长度无关)

我认为您的意图是无条件地读length中的Load一次,而不是在那里循环浏览。