C++ C++;以二进制模式写入和读取文件

C++ C++;以二进制模式写入和读取文件,c++,c++11,fstream,C++,C++11,Fstream,我想在/dev/shm/upload中创建一个二进制文件,然后以二进制模式打开一个文件并将数据写入其中 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_pat

我想在
/dev/shm/upload
中创建一个二进制文件,然后以二进制模式打开一个文件并将数据写入其中

    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;
    }
std::string_path=“/dev/shm/upload/”;
std::string filename=“download_file.out”;
std::string tmpStr=“敏捷的棕色狐狸跳过懒惰的狗”;
createFile(字符串路径、文件名);
bool createFile(std::string和string_路径,std::string和文件名){
std::string命令\u string=“mkdir-p”;
命令\u string.append(字符串\u路径);

正如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;
}
#包括
使用名称空间std;
int main()
{
string tmpStr=“敏捷的棕色狐狸跳过懒惰的狗\n”;
流outbinfile(“output_binary.txt”,std::ios::binary | std::ios::out);
对于(int i=0;i
  • xfstream(fn,ios::text)
    xfstream(fn,ios::binary)
    (其中x是
    i
    o
    )之间的区别在于如何插入/提取行尾。

    • 对于文本流,
      在这个函数中是
      没有类型名或类的模板函数意味着什么?
      -这不是文本和二进制模式之间的主要区别换行符转换?字符串代表文本,不管你是否以二进制模式写。它仍然会显示为人类可读的文本。如果你想看到差异,试着写一个数字。
      binary\u write\u string
      应该是
      binary\u write
      ,前者不会编译,因为它是一个专门化的函数,不会编译exist@Azad事实并非如此。这取决于编码和endianess,后者可能导致二进制和文本之间的不同输出模式
      #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;
      }
      
      #include <iostream>
      #include <iomanip>
      using namespace std;
      //...
      cout << setfill( '~' ) << setw( 2 ) << 2; // outputs "~2"
      
      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"