c – 随机访问文件无法正常工作

我正在尝试编写这个程序,将员工数据库保存在随机访问文件中,它必须具有添加员工和删除员工的功能(通过写入记录中的所有空格).这是我到目前为止所做的,但它不能正常工作.在阅读员工时,它会读取正确记录的工资,但会记录下一条记录的名称.此外,当我删除最后一条记录并在该记录中添加员工时,我无法查看员工信息,我收到异常错误.

我不是在寻找解决方案,只是在正确的方向上轻推.谢谢.

#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,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).首先读取你想要的记录,第二个读取下一个记录(标准没有指定它们中的哪一个是’第一个’).

相关文章

首先GDB是类unix系统下一个优秀的调试工具, 当然作为debug代...
1. C语言定义1个数组的时候, 必须同时指定它的长度.例如:int...
C++的auto关键字在C+⬑新标准出来之前基本...
const关键字是用于定义一个不该被改变的对象,它的作用是告诉...
文章浏览阅读315次。之前用C语言编过链表,这几天突然想用C+...
文章浏览阅读219次。碰到问题就要记录下来,防止遗忘吧。文章...