简体   繁体   中英

Expecting eof but instead arbitrary data near eof

I have sought to discover the cause of unwanted trailing end-data in a file I am writing specific data into and do not believe I have made errors in writing to the file.

The output looks like:

building     room_numbr   capacity

packard    | 101        | 500        |
painter    | 514        | 10         |
ÿÿÿÿÿÿÿÿÿÿ | Attempt to seek file pointer error

The Attempt to seek file pointer error is normal as it represents a thrown exception when attempting to move the file pointer on an invalid stream. However, the ÿÿÿÿÿÿÿÿÿÿ is not normal nor expected in a fixed size file format using either 10 or 20 bytes to write data.

Create file here:

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;
    }   

}

Write data to file

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;
}
}

Read data file here:

   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;
}

}

Code that reads the file and prints the result to the screen:

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 returns std::char_traits<char>::eof on failure, which usually has the int value -1 . If you interpret that blindly as a valid character and cast to char , you will get '\\xff' , which in ISO-8859-15 is ÿ .

You should be checking for eof and/or eofbit when you read characters from the file, and especially after seeking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM