假设您有一个包含以下内容的文件:
当我想阅读随机记录时,我使用此功能:
int FixedLengthRecordFile :: read (int numRec, FixedLengthFieldsRecord & rec)
问题是,为了使这个功能起作用,我必须事先创建一个FixedLengthFieldsRecord
,这意味着要指定这条记录所拥有的字段数......这不是我想要的。我希望文件处理程序足够“智能”以识别记录有多少字段,并在读取时动态创建FixedLengthFieldsRecord
。
我怎样才能做到这一点?该函数必须返回int
,因为我将其用作退出代码(错误/成功)。
答案 0 :(得分:2)
可能您可以定义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];
}
答案 1 :(得分:0)
FixedLengthRecordFile
应该打开文件(或者更好,在构造函数中取std::istream
),读取标题,然后FixedLengthRecordFile::read(...)
成员函数可以使用std::istream::seekg
和std::istream::read
获取数据。
现在,针对您的实际问题。为什么您希望FixedLengthRecordFile::read
函数在通过引用获取记录时返回int
?不会像
FixedLengthFieldsRecord FixedLengthRecordFile::read(size_t numRec, int& foo);
更容易吗?
如果您坚持原始签名,请使FixedLengthFieldsRecord
默认构造函数将对象初始化为无效状态(例如,添加设置为bool isValid
的{{1}}标志),然后添加一个false
成员函数,然后可以由setData(size_t length, const char* data)
函数调用。