C++ Writing to file vector of byte

I have:

typedef unsigned char;
std::vector<byte> data;

I tried to save data in file this way (but I have error):

fstream file(filename,ios::out);
file.write(&data, data.size());

How to process or cast data to write it in file.

To store a vector in a file, you have to write the contents of the vector, not the vector itself. You can access the raw data with &vector[0], address of the first element (given it contains at least one element).

ofstream outfile(filename, ios::out | ios::binary); 
outfile.write(&data[0], data.size());

This should be fairly efficient at writing. fstream is generic, use ofstream if you are going to write.

C Programming Tutorial for Beginners, Learn C the Hard Way: Practical Exercises on the Computational Subjects You Keep Avoiding (Like C) C or c is the third letter in the English and ISO basic Latin alphabets.Its name in English is cee (pronounced / ˈ s iː /), plural cees.

Use vector::data to get a pointer the the underlying data:

file.write(data.data(), data.size());

"C" Programming Language: Brian Kernighan, This course will give you a full introduction into all of the core concepts in the C programming Duration: 3:46:13 Posted: Aug 15, 2018 C is a successor of B language which was introduced around the early 1970s. The language was formalized in 1988 by the American National Standard Institute (ANSI). The UNIX OS was totally written in C. Today C is the most widely used and popular System Programming Language.

*Statement file.write(&buffer[0],buffer.size()) makes error:

error C2664: 'std::basic_ostream<_Elem,_Traits>::write' : cannot convert parameter 1 from 'unsigned char *' to 'const char *'

*In my compiler (VS2008) I don't have data() method for vector.

I think below is correct:

file.write((const char*)&buffer[0],buffer.size());

Why C is so Influential - Computerphile, Computerphile. Computerphile. 1.75M subscribers. "C" is one of the most widely used Duration: 8:26 Posted: Aug 18, 2015 C is a procedural programming language. It was initially developed by Dennis Ritchie as a system programming language to write operating system. The main features of C language include low-level access to memory, simple set of keywords, and clean style, these features make C language suitable for system programming like operating system or compiler development.

You are to pass the address of the first element, not the address of the vector object itself.

     &data[0]

Note: Make sure that the vector is not empty before doing this.

Learn C, Why is C such an influential language? We asked ardent C fan Professor Brailsford. Brian Duration: 10:50 Posted: Aug 25, 2017 This is a list of operators in the C and C++ programming languages.All the operators listed exist in C++; the fourth column "Included in C", states whether an operator is also present in C. Note that C does not support operator overloading.

A lot of these solutions are only partially complete (lacking includes & casts), so let me post a full working example:

#include <vector>
#include <fstream>

int main()
{
    std::vector<std::byte> dataVector(10, std::byte{ 'Z' });

    const std::string filename = "C:\\test_file.txt";

    std::ofstream outfile(filename, std::ios::out | std::ios::binary);
    outfile.write(reinterpret_cast<const char*>(dataVector.data()), dataVector.size());

    return 0;
}

C Tutorial, learn-c.org is a free interactive C tutorial for people who want to learn C, fast. As well as C and Simula's influences, other languages also influenced this new language, including ALGOL 68, Ada, CLU and ML . Initially, Stroustrup's "C with Classes" added features to the C compiler, Cpre, including classes, derived classes, strong typing, inlining and default arguments.

C Language - Overview, C programming is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone� C# (pronounced see sharp, like the musical note C♯, but written with the number sign) is a general-purpose, multi-paradigm programming language encompassing strong typing, lexically scoped, imperative, declarative, functional, generic, object-oriented (class-based), and component-oriented programming disciplines.

Learn C Programming, C is a powerful general-purpose programming language. Our C tutorials will guide you to learn C programming one step at a time with the help of examples. Programming Languages Development - C++ has been used extensively in developing new programming languages like C#, Java, JavaScript, Perl, UNIX’s C Shell, PHP and Python, and Verilog etc. Computation Programming - C++ is the best friends of scientists because of fast speed and computational efficiencies.

Cprogramming.com: Learn C and C++ Programming, The best site for C and C++ programming. Popular, beginner-friendly C and C++ tutorials to help you become an expert! Microsoft C++, C, and Assembler documentation. Learn how to use C++, C, and assembly language to develop applications, services, and tools for your platforms and devices.

Comments
  • This code is wrong - it won't compile.
  • data.size is a problem. Its not function so it shoudld be data.size()
  • Note, that with C++11 &data[0] may be replaced with data.data() which looks a little cleaner.
  • A cast is required - the first parameter of istream::write is const char *, however in OP code, &data[0] is unsigned char *
  • With this approach one should point out, that the latter argument represents the number of bytes to be written. As sizeof(char) == 1 this is ok. For any other datatype use file.write((char*) data.data(), data.size() * sizeof(data.front())). As @staticx pointed out, make sure to add the appropriate flags when opening the ofstream: ofstream file(filename, ios::out | ios::binary);
  • You use it like this file.write(reinterpret_cast<const char*>(&myVector[0]), myVector.size());
  • data() method is there only in C++11 so in VS2008 it won't b there.
  • If you're going to cast, then why not cast to the type mentioned in the error message? Also, you only think it is correct? Is it helpful to every future viewer of this question if an answer only thinks itself correct and hasn't actually verified it? I'm not trying to put you down; just trying to improve the answer.
  • @Kaz Dragon: So write your answer and my alternative version and I will accept it. I corrected my answer.
  • Actually, hadn't noticed that you're also the question poster. You can disregard half of what I said.