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.


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.