std :: fstream :: write && std :: fstream :: putc std :: ios :: binary(std::fstream::write && std::f

编程入门 行业动态 更新时间:2024-10-24 19:27:56
std :: fstream :: write && std :: fstream :: putc std :: ios :: binary(std::fstream::write && std::fstream::putc std::ios::binary)

我怀疑我甚至没有使用std::fstream为二进制I / O创建和打开文件

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::binary); // create file if(!binary_search_file.is_open()){ binary_search_file.clear(); binary_search_file.open(data_file_name, std::ios::out | std::ios::binary); binary_search_file.close(); binary_search_file.open(data_file_name), std::ios::out | std::ios::in | std::ios::binary | std::ios::ate; } 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){ binary_search_file.write(attribute.c_str(), 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; } }

但该文件是具有可读文本数据的标准文本文件。 我想以二进制格式(2字节字符)写一个字符串或字符本身的字符。 我试图操作类似于RandomAccessFile的std::fstream 。

_ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ __

问题是:我是否正确创建了文件,为什么我没有看到写入的二进制数据?

I suspect I am not even creating and opening a file for binary I/O using std::fstream

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::binary); // create file if(!binary_search_file.is_open()){ binary_search_file.clear(); binary_search_file.open(data_file_name, std::ios::out | std::ios::binary); binary_search_file.close(); binary_search_file.open(data_file_name), std::ios::out | std::ios::in | std::ios::binary | std::ios::ate; } 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; } }

I believe this is true because I am writing data

void BinarySearchFile::writeT(std::string attribute){ try{ if(binary_search_file){ binary_search_file.write(attribute.c_str(), 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; } }

But the file is a standard text file with readable text data. I want to write the characters of a string or characters themselves in binary format (2 byte char). I am attempting to operate a std::fstream similar to a RandomAccessFile.

_________________________________________________________________________________

Question is: Did I create the file correctly and why am I not seeing binary data written?

最满意答案

您确实正确地创建了文件。 你有一个误解,有两种文件,二进制和文本。 相反,有两种I / O操作,像operator<<这样的文本和像write这样的二进制文件。

您没有看到两个字节字符的原因是std::string只有一个字节字符。 如果你想要两个字节的字符使用std::wstring 。

You did create the file correctly. You have a misunderstanding that there are two kinds of files, binary and text. Instead there are two kinds of I/O operations, text like operator<< and binary like write.

The reason that you aren't seeing two byte chars is that std::string only has one byte chars. If you want two byte chars use std::wstring.

更多推荐

本文发布于:2023-07-29 15:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1317592.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:write   fstream   std   binary   ios

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!