0

I want to create a binary file in /dev/shm/uploaded and open a file in binary mode and write data into it.

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

I suspect that using << operator I am writing the data in text mode, rather than in binary mode. I want to write the data in binary mode.

I was looking at binary read and write

It has a function as follows

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

In this function what does a templated function without typename or class mean? Is this the correct approach.

6
  • In this function what does a templated function without typename or class mean? - It's a template specialization Commented Dec 23, 2019 at 8:09
  • 3
    Isn't the main difference between text and binary mode the newline conversion? Commented Dec 23, 2019 at 8:14
  • A string represents a text, and it doesn't matter whether you write in binary mode or not. it would still appear as a human readable text. Try writing a number if you want to see the difference. Commented Dec 23, 2019 at 8:14
  • binary_write_string should be binary_write, the former won't compile as it is a specialisation of a function that doesn't exist Commented Dec 23, 2019 at 8:16
  • @Azad That is actually not true. It depends on the encoding and also endianess that can result in different outputs between binary and text mode. Commented Dec 23, 2019 at 8:24

2 Answers 2

1

As Botje suggests, the main difference between text and binary mode is the newline conversion. You can try the following code and see the output.

#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 is 132 bytes, as expected. But output.txt is 135 bytes in Windows. Because for newline, it writes out \r\n actually.[1]

Sign up to request clarification or add additional context in comments.

Comments

1
  1. The difference between xfstream( fn, ios::text ) and xfstream( fn, ios::binary ) (where x is i or o) is how the end-of-line is inserted/extracted.
    • For a text stream is << '\n' will insert (depending on OS) \n\r. On extracting, the sequence will be translated back to \n.
    • For a binary stream, what you insert/extract is what you write/read.
  2. Opening a stream in binary mode and writing/reading binary data to/from it are different things. When you use the insertion/extraction operators (<< & >>) you write/read formatted data (similar to printf in c):

    #include <iostream> #include <iomanip> using namespace std; //... cout << setfill( '~' ) << setw( 2 ) << 2; // outputs "~2" 
  3. If you want to write/read the actual bytes (the 4 bytes of a 32bit integer, for instance, not its human readable form) you must use ostream::write/istream::read. c++ wont stop you from using these functions with a text stream. It is your responsibility to combine them correctly.

  4. In c++, template functions may be specialized: exhibit different behavior for specific template signatures. What you missed from the referred link is the unspecialized version of the function.

    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" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.