期望eof,而不是eof附近的任意数据
Expecting eof but instead arbitrary data near eof
我试图发现我正在写入特定数据的文件中不需要的尾随结束数据的原因,并且不相信我在写入文件时犯了错误。
输出如下:
building room_numbr capacity
packard | 101 | 500 |
painter | 514 | 10 |
ÿÿÿÿÿÿÿÿÿÿ | Attempt to seek file pointer error
Attempt to seek file pointer error
是正常的,因为它表示当试图在无效流上移动文件指针时抛出异常。然而,ÿÿÿÿÿÿÿÿÿÿ
不是正常的,也不是固定大小的文件格式,使用10或20字节来写数据。
创建文件:
BinarySearchFile::BinarySearchFile(std::string file_name){
// concatenate extension to fileName
file_name += ".dat";
// form complete table data filename
data_file_name = file_name;
// create or reopen table data file for reading and writing
binary_search_file.open(data_file_name, std::ios::out | std::ios::in | std::ios::app);
if(!binary_search_file.is_open()){
binary_search_file.clear();
binary_search_file.open(data_file_name, std::ios::out);
binary_search_file.close();
binary_search_file.open(data_file_name, std::ios::out | std::ios::in | std::ios::app);
}
try{
if(binary_search_file.fail()){
throw CustomException("Unspecified table data file error");
}
}
catch (CustomException &custom_exception){ // Using custom exception class
std::cout << custom_exception.what() << std::endl;
return;
}
}
写入文件
void BinarySearchFile::writeT(std::string attribute){
try{
if(binary_search_file){
for(auto start = attribute.begin(); start != attribute.end(); ++start){
binary_search_file.put(' ');
binary_search_file.put(*start);
}
binary_search_file.flush();
/*
attribute.resize(attribute.length() * 2);
const char *write_this = attribute.data();
binary_search_file.write(write_this, attribute.length());
*/
}else if(binary_search_file.fail()){
throw CustomException("Attempt to write attribute error");
}
}
catch(CustomException &custom_exception){ // Using custom exception class
std::cout << custom_exception.what() << std::endl;
return;
}
}
读取数据文件:
std::string BinarySearchFile::readT(long file_pointer_location, long size_of_data)
{
try{
if(binary_search_file){
std::string data = "";
binary_search_file.seekp(file_pointer_location);
binary_search_file.seekg(file_pointer_location);
while (size_of_data > 0 ){
binary_search_file.get();
data += binary_search_file.get();
size_of_data -= 2;
}
/*
char data[20];
binary_search_file.seekp(filePointerLocation);
binary_search_file.seekg(filePointerLocation);
binary_search_file.read(data, sizeOfData);
*/
return data;
}else if(binary_search_file.fail()){
throw CustomException("Attempt to read attribute error");
}
}
catch(CustomException &custom_exception){ // Using custom exception class
std::cout << custom_exception.what() << std::endl;
}
}
读取文件并将结果打印到屏幕上的代码:
while(true){
//reinitialize the catalog pointer to the beginning
catalog->setPointerBegin();
//display data
do{
if (boost::iequals((domain = catalog->getAttributeDomain()), "string")){
if(dataFile->binary_search_file_status()){
std::cout << dataFile->read_data(filePointer, 20) << " | ";
if (!writer_.fail())
writer_ << dataFile->read_data(filePointer, 20) << " | ";
}
else{
std::cout << "n";
if (!writer_.fail())
writer_ << "n";
return true;
}
// update the file pointer
filePointer += 20;
dataFile->set_file_pointer(filePointer);
}
else{
if(dataFile->binary_search_file_status()){
std::cout << dataFile->read_data(filePointer, 10);
if (!writer_.fail())
writer_ << dataFile->read_data(filePointer, 10);
for(int i = 0; i < 5; i++){
std::cout << " ";
if (!writer_.fail())
writer_ << " ";
}
std::cout << " | ";
if (!writer_.fail()){
writer_ << " | ";
}
}
else{
std::cout << "n";
if (!writer_.fail()){
writer_ << "n";
}
return true;
}
// update the file pointer
filePointer += 10;
}
} while(catalog->traverseForward() != nullptr);
std::cout << "n";
if (!writer_.fail())
writer_ << "n";
}
}
std::ifstream::get
失败返回std::char_traits<char>::eof
,通常int
的值为-1
。如果盲目地将其解释为有效字符并强制转换为char
,则将得到'xff'
,即ISO-8859-15中的ÿ
。
当你从文件中读取字符时,你应该检查eof
和/或eofbit
,特别是在寻找。
相关文章:
- 防止主数据类型C++的隐式转换
- 如何确定我已使用非编码文件到达 EOF?
- 用于访问容器<T>数据成员的正确 API
- 嵌套在类中时无法设置成员数据
- 使用流处理接收到的数据
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 在cuda线程之间共享大量常量数据
- C++将文本文件中的数据读取到结构数组中
- 如何在C++中序列化结构数据
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 在c代码之间共享数据的最佳方式
- 链表,反向函数,数据结构
- 数据成员SFINAE的C++17测试:gcc vs clang
- C++浮点数据类型和字符串数据类型无法子到模板函数中
- 如何对点云数据进行排序
- 而(getline(fin,str)){}即使在到达eof之后仍在处理数据
- 期望eof,而不是eof附近的任意数据
- 你能通过查看过去的EOF来创建未定义的数据吗