c++ - 从具有固定长度记录的文件中读取

标签 c++ file fixed-length-record

假设您有一个包含以下内容的文件:

  • 文件头,包含:a) 第一个空记录编号,b) 每条记录的大小,以及 c) 文件可以容纳的最大记录数。
  • 记录包含:a) 指示记录是空闲还是已占用的字节,b) 固定数量的字段。

当我想读取随机记录时,我使用这个函数:

int FixedLengthRecordFile :: read (int numRec, FixedLengthFieldsRecord & rec) 

问题是,为了使这个功能起作用,我必须事先创建一个 FixedLengthFieldsRecord,这意味着指定该记录具有的字段数...这不是我想要的想。我希望文件处理程序足够“智能”以识别记录有多少字段,并在读取记录时即时创建 FixedLengthFieldsRecord

我怎样才能做到这一点?该函数必须返回一个 int,因为我将它用作退出代码(错误/成功)。

最佳答案

可能,您可以定义 FixedLengthFieldsRecord 类,以便类本身强制执行正确的记录长度,但允许每条记录中的任意数量的字段。实现此目的的一种可能的好方法是让 CONSTRUCTOR 从文件中读取下一条记录。

类(减去一些必要的错误检查,你可以添加)可以用类似的方式编写如下(这个类假设你在创建这个类的对象之前阅读了文件的第一行):

using namespace std;
class FixedLengthFieldsRecord
{

    public:

        FixedLengthFieldsRecord(int const recordLength, istream & s); // Set the length of the record in the constructor
        bool IsEmpty() const;
        int FieldCount() const; // variable number of fields allowed; but LENGTH of record is enforced (see below)

        bool IsValidRecord(); // Does the record contain the correct number of bytes?
        string GetField(int const index) const; // This could throw an exception if the record is not valid

    protected:
        // Could have sophisticated functions here to replace fields, remove fields, reorder fields, etc.

    // This section contains the actual implementation.
    private:
        vector<string> fields; // The vector contains a VARIABLE number of fields
        bool is_empty;
        bool is_valid;
        int const record_length; // This contains the LENGTH of the record; it is set in the constructor and cannot be changed

    // The following variable and function store (and access) ALL the records
    static vector<FixedLengthFieldsRecord> records;
    static read(int numRec, FixedLengthFieldsRecord & rec);

}

FixedLengthFieldsRecord::FixedLengthFieldsRecord(int const recordLength_, istream & s)
    : record_length(recordLength)
{
    // pseudocode provided here
    // this functionality could be factored into other functions that are called by the constructor

    is_valid = true;

    is_empty = ReadFirstByte(s); // ReadFirstByte (not shown) reads first byte of current line and returns true if it indicates an empty record

    if (!is_empty)
    {
        string field;
        int currentRecordLength = 0;
        while (ReadNextField(s, field)) // ReadNextField() returns true if another field was read from the line (i.e., not end-of-line
        {
            currentRecordLength+= field.length();
        }
        if (currentRecordLength != record_length)
        {
            is_valid = false;
        }
        if (currentRecordLength > record_length)
        {
            break;
        }
        if (is_valid)
        {
            fields.push_back(field);
        }
    }
    if (is_valid)
    {
        records.push_back(*this); // not efficient, nor thread safe - deep copy occurs here
    }
}

bool FixedLengthFieldsRecord::IsEmpty()
{
    return is_empty();
}

bool FixedLengthFieldsRecord::IsValidRecord()
{
    return is_valid;
}

string FixedLengthFieldsRecord::GetField(int const index)
{
    if (!is_valid)
    {
        // throw an exception, or return an empty string
    }
    if (index < 0 || index >= fields.size())
    {
        // throw an exception, or return an empty string
    }
    return fields[index];
}

FixedLengthFieldsRecord::read(int numRec, FixedLengthFieldsRecord & rec)
{
    if (numRec < 0 || numRec >= records.size())
    {
        // throw an exception, or just return
    }
    rec = records[numRec];
}

关于c++ - 从具有固定长度记录的文件中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15777110/

相关文章:

c++ - 在其他函数中设置结构成员的值

c++模板-类型/值不匹配-使用依赖于派生类的类型实例化基类模板

java - 将序列化对象写入垃圾文件?

C++ 二进制文件读取问题

c++ - 有一个构造函数,但有两个析构函数

c++ - 谷歌测试 : Parameterized tests which use an existing test fixture class?

Python多线程文件处理

c# - 如果值为空格,则不会调用 FileHelpers FieldConverter

用于固定长度文本文件的 .NET 库