【问题标题】:C++ write and read a file in binary modeC++ 以二进制模式写入和读取文件
【发布时间】:2019-12-23 08:04:22
【问题描述】:

我想在/dev/shm/uploaded中创建一个二进制文件,并以二进制模式打开一个文件并写入数据。

    std::string string_path = "/dev/shm/uploaded/";
    std::string filename = "download_file.out";
    std::string tmpStr = "The quick brown fox jumps over the lazy dog";

    createFile(string_path, filename); 

    bool createFile(std::string &string_path, std::string &filename) {
        std::string command_string = "mkdir -p ";
        command_string.append(string_path);
        std::cout << command_string << std::endl;
        int check = system(command_string.c_str());
        if(-1 == check) {
           return false;
        }
        std::ofstream outfile(string_path + filename, std::ios::binary | std::ios::out);
        if(outfile.is_open()) {
          for (int i = 0; i < 100000; i++) {
              outfile << tmpStr;
          }
        }
        outfile.close();
        return true;
    }

我怀疑使用&lt;&lt; 运算符我正在以文本模式而不是二进制模式写入数据。 我想以二进制模式写入数据。

我在看binary read and write

它的作用如下

template<>
std::ostream& binary_write_string(std::ofstream& stream, const std::string& value){
    return stream->write(value.c_str(), value.length());
}

在这个函数中,没有typenameclass 的模板函数是什么意思?这是正确的方法吗。

【问题讨论】:

  • In this function what does a templated function without typename or class mean? - 这是一个template specialization
  • 文本和二进制模式的主要区别不就是换行吗?
  • 一个字符串代表一个文本,不管你是否写成二进制模式。它仍然会显示为人类可读的文本。如果您想查看差异,请尝试写一个数字。
  • binary_write_string 应该是binary_write,前者不会编译,因为它是一个不存在的函数的特化
  • @Azad 这实际上不是真的。这取决于编码和字节序,这会导致二进制和文本模式之间的不同输出。

标签: c++ c++11 fstream


【解决方案1】:

正如 Botje 所建议的,文本模式和二进制模式之间的主要区别在于换行符转换。您可以尝试以下代码并查看输出。

#include <fstream>

using namespace std;

int main()
{
    string tmpStr = "The quick brown fox jumps over the lazy dog\n";

    ofstream outbinfile("output_binary.txt", std::ios::binary | std::ios::out);
    for (int i=0; i<3; i++)
        outbinfile << tmpStr;
    outbinfile.close();

    ofstream outfile("output.txt", std::ios::out);
    for (int i=0; i<3; i++)
        outfile << tmpStr;
    outfile.close();

    return 0;
}

output_binary.txt 是 132 字节,正如预期的那样。但是output.txt 在 Windows 中是 135 个字节。因为对于换行符,它实际上写出了\r\n[1]

【讨论】:

    【解决方案2】:
    1. xfstream( fn, ios::text )xfstream( fn, ios::binary )(其中 x 是 io)之间的区别在于如何插入/提取行尾
      • 对于文本流is &lt;&lt; '\n' 将插入(取决于操作系统)\n\r。提取时,序列将翻译回\n
      • 对于二进制流,您插入/提取的内容就是您写入/读取的内容
    2. 以二进制模式打开流和向其中写入/读取二进制数据是不同的事情。当您使用插入/提取运算符(&lt;&lt; & &gt;&gt;)时,您写入/读取格式化数据(类似于 c 中的 printf

      #include <iostream>
      #include <iomanip>
      using namespace std;
      //...
      cout << setfill( '~' ) << setw( 2 ) << 2; // outputs "~2"
      
    3. 如果您想写入/读取实际字节(例如,32 位整数的 4 个字节,而不是人类可读的形式),您必须使用 ostream::write/@987654333 @。 c++ 不会阻止您将这些函数与文本流一起使用。正确组合它们是您的责任。

    4. 在 c++ 中,模板函数可能是专用:针对特定模板签名表现出不同的行为。 您从所引用的链接中错过的是该函数的非专业版本

      template<typename T> void f( T )
      {
        cout << "unspecialized\n";
      }
      
      template<> void f( const char* s )
      {
        cout << "specialized\n";
      }
      //...
      f( 0 ); // prints "unspecialized"
      f( 'c' ); // prints "unspecialized"
      f( "" ); // prints "specialized"
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-07
      相关资源
      最近更新 更多