gpt4 book ai didi

c++ - 期待 eof 但 eof 附近的任意数据

转载 作者:行者123 更新时间:2023-11-28 00:45:41 25 4
gpt4 key购买 nike

我试图找出我正在向其中写入特定数据的文件中不需要的尾随结束数据的原因,并且不相信我在写入文件时出错。

输出如下:

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 但 eof 附近的任意数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16365680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com