随机访问文件无法正常工作
Random access file not working properly
我正试图编写一个程序,将员工数据库保存在一个随机访问文件中,它必须具有添加员工和删除员工的功能(通过在记录中写入所有空格)。这就是我目前为止所拥有的,但它并不完全正确。读取员工时,它读取正确记录的工资,但读取下一条记录的名称。此外,当我删除最后一条记录并在该记录中添加员工时,我无法查看员工信息,我会收到一个异常错误。
我不是在这里寻找解决方案,只是朝着正确的方向前进。谢谢
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include "ccc_empl.h"
using namespace std;
const int NEWLINE_LENGTH = 2;
const int RECORD_SIZE = 30 + 10 + NEWLINE_LENGTH;
/**
converts a string to a floating-point value
@param s a string representing a floating-point value
@return the equivalent floating-point value
*/
double string_to_double(string s)
{
istringstream instr(s);
double x;
instr >> x;
return x;
}
/*
reads an employee record from the input file
@param e the employee
@param in the file to read from
*/
Employee read_employee(istream& in)
{
string line;
getline(in, line);
string input_name = line.substr(0, 30);
double input_salary = string_to_double(line.substr(30, 10));
Employee e(input_name, input_salary);
return e;
}
/*
gets input for an Employee object
@param input_name the name of the employee
@param input_salary the salary of the employee
@param e the Employee object
@return returns the Employee object
*/
Employee input_employee()
{
string input_name;
cout << "Name: ";
cin.ignore();
getline(cin, input_name);
cout << "Salary: ";
double input_salary;
cin >> input_salary;
Employee e(input_name, input_salary);
return e;
}
/**
adds an employee record to a file
@param e the employee record to write
@param out the file to write to
*/
void add_employee(Employee e, ostream& out)
{
out << e.get_name()
<< setw(30)
<< fixed << setprecision(2)
<< e.get_salary();
}
/**
removes an employee record from a file
@param e the employee record to remove
@param out the file to remove from
*/
void remove_employee(ostream& out)
{
out << " " << setw(42) << fixed << setprecision(2) << " n";
}
int main()
{
cout << "Please enter the data file name: ";
string filename;
cin >> filename;
fstream fs;
fs.open(filename);
fs.seekg(0, ios::end); // Go to end of file
int nrecord = fs.tellg() / RECORD_SIZE; // determine number of records in the file
int menu_input = 0;
string input_name;
double input_salary = 0;
while (menu_input != 4)
{
cout << "Please enter the record to update: (0 - " << nrecord - 1 << ") exit to quit ";
int pos = 0;
cin >> pos;
if(cin.fail())
{
cout << "Exiting..." << endl;
system("pause");
return 0;
}
// menu for user input
cout << "nWhat action would you like to perform?" << endl;
cout << "Add employee.....1" << endl;
cout << "Remove employee..2" << endl;
cout << "View employee....3" << endl;
cin >> menu_input;
switch(menu_input)
{
case 1: fs.seekg(pos * RECORD_SIZE, ios::beg);
add_employee(input_employee(), fs);
break;
case 2: fs.seekg(pos * RECORD_SIZE, ios::beg);
remove_employee(fs);
break;
case 3: fs.seekg(pos * RECORD_SIZE, ios::beg);
cout << "nName: " << read_employee(fs).get_name() << "Salary: " << read_employee(fs).get_salary() << endl << endl;
break;
default: cout << "Invalid entry" << endl;
break;
}
}
fs.close();
system("pause");
return 0;
}
好的,这是新的和改进的(好吧,我想是的)代码。我唯一的问题是,在添加员工时,我可以让程序将其添加到第一个打开的记录中,但如果没有打开的记录,我似乎无法将其添加在文件末尾,而不会将添加到第一条空记录中。我的意思是,如果有一个空记录,它会将员工添加到记录中,但如果没有空记录,则不会将该员工添加到文件的末尾。如果我添加要添加到文件末尾的代码,会发生以下两种情况之一:要么添加到空记录,然后请求另一名员工并将其添加到末尾,要么跳过空记录并添加到文件结尾。
不确定我在这里做错了什么,但任何暗示都将不胜感激。
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include "ccc_empl.h"
using namespace std;
const int NEWLINE_LENGTH = 2;
const int RECORD_SIZE = 30 + 10 + NEWLINE_LENGTH;
/**
converts a string to a floating-point value
@param s a string representing a floating-point value
@return the equivalent floating-point value
*/
double string_to_double(string s)
{
istringstream instr(s);
double x;
instr >> x;
return x;
}
/*
reads an employee record from the input file
@param e the employee
@param in the file to read from
*/
Employee read_employee(istream& in)
{
string line;
getline(in, line);
string input_name = line.substr(0, 30);
double input_salary = string_to_double(line.substr(30, 10));
Employee e(input_name, input_salary);
return e;
}
/*
gets input for an Employee object
@param input_name the name of the employee
@param input_salary the salary of the employee
@param e the Employee object
@return returns the Employee object
*/
Employee input_employee()
{
string input_name;
cout << "Name: ";
cin.ignore();
getline(cin, input_name);
cout << "Salary: ";
double input_salary;
cin >> input_salary;
Employee e(input_name, input_salary);
return e;
}
/**
adds an employee record to a file
@param e the employee record to write
@param out the file to write to
*/
void add_employee(Employee e, ostream& out)
{
out << e.get_name()
<< setw(10 + 30 - e.get_name().length())
<< fixed << setprecision(2)
<< e.get_salary() << "n";
}
/**
removes an employee record from a file
@param e the employee record to remove
@param out the file to remove from
*/
void remove_employee(ostream& out)
{
out << " " << setw(40) << fixed << setprecision(2) << " n";
}
int main()
{
cout << "Please enter the data file name: ";
string filename;
cin >> filename;
fstream fs;
fs.open(filename);
fs.seekg(0, ios::end); // Go to end of file
int nrecord = fs.tellg() / RECORD_SIZE; // determine number of records in the file
int menu_input = 1;
string input_name;
double input_salary = 0;
while (menu_input)
{
// menu for user input
cout << "nWhat action would you like to perform?" << endl;
cout << "Add employee.....1" << endl;
cout << "Remove employee..2" << endl;
cout << "View employee....3" << endl;
cout << "Exit.............4" << endl;
cin >> menu_input;
if (menu_input == 4)
{
cout << "nExiting..." << endl << endl;
system("pause");
return 0;
}
// switch statment to perform selected menu_input task
switch(menu_input)
{
case 1: // adds an employee in the first empty record
// or at the end of the file if no records are empty
{
int count = 0;
string s;
for (int i = 0; i < nrecord; i++)
{
fs.seekp(count, ios::beg);
getline(fs, s);
if (isspace(s[0]))
{
fs.seekp(count, ios::beg);
add_employee(input_employee(), fs);
nrecord++;
break;
}else
count += 42;
}
}
break;
case 2: {
cout << "Please enter the record to remove: (0 - " << nrecord -1 << ") ";
int pos = 0;
cin >> pos;
fs.seekp(pos * RECORD_SIZE, ios::beg);
remove_employee(fs);
}
break;
case 3: {
cout << "Please enter the record to view: (0 - " << nrecord -1 << ") ";
int pos = 0;
cin >> pos;
fs.seekg(pos * RECORD_SIZE, ios::beg);
Employee e(read_employee(fs));
cout << "nName: " << e.get_name() << "Salary: " << e.get_salary() << endl << endl;
}
break;
default: cout << "Invalid entry" << endl;
break;
}
}
fs.close();
system("pause");
return 0;
}
您调用read_employee(fs)
两次。第一个读取您想要的记录,第二个读取下一个记录(标准不指定哪一个是"第一个")。
似乎从未在add_employe中添加行终止符,read_employE被调用两次。只需缓存函数的结果。
我知道这只是家庭作业,但我认为你可以改进这个程序。
-
数据库是否需要在运行时作为文件保留?您可以将整个文件读取到Employee数组中,并在最后重写它。它不会限制员工姓名的长度。
-
这个程序不是跨平台的。您不应该依赖文件大小来确定数据库中的条目数量,因为行终止符因操作系统而异。相反,请阅读所有内容,并计算条目。
-
只有窗口有"暂停"程序。为了达到同样的效果,只需在程序结束时读取stdin和,并忽略输入。
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- 为什么STD ::计数将常数传递给Lambda,而不是在弦上工作时而不是字符
- sdl软件渲染器不工作,工作在硬件加速的一个
- C++程序已停止工作-求解常微分方程