Write Vector to File in C++

This article discusses different methods to Write Vector to File in C++.

Vectors in C++ are sequential containers similar to arrays. However, vectors need to have a fixed size.

On the other hand, files facilitate the storage of data permanently to the disk, therefore, mitigating the data loss even after the program stops execution.

Write Vector to File in C++ using vector indices

You can traverse all the elements of a vector one by one using the indices. Writing to the file is similar to writing to the console output except that you need to open a file output stream.

You can open the file using the fstream library’s open() function. The definition of the open() function is given below.

The function accepts the pathname of the file and the mode of opening it. You can read more about modes of opening a file in C++ here.

To write the vector to the file,

  • Open the file.
  • Traverse each element of the vector using indices in a loop,
    • Write each element to the file (using << operator).
  • Close the file by calling the close() function.

You can get the number of elements in the vector by calling the size() function of the vector class.

Let us see the code.

Note that the program writes a special newline character to the file after each vector element for formatting purposes.

Output (from the file):

Hello!
We are Java2Blog.
You can a lot from our articles.

Write Vector to File in C++ Using Iterator

An iterator points to a specific memory location of the vector. Iterators are similar to pointers and you can use them to access the elements of a vector.

You can define an iterator to your vector as given below.

You can also use the auto keyword that automatically defined iterator for your vector. The code example in this section uses the auto keyword.

After opening a file, you can write a vector to it by following the steps given below.

  • Get the starting iterator by calling begin() function.
  • Get the final iterator by calling the end() function.
  • Traverse from starting to the ending iterator using a loop,
    • Write each element to file by dereferencing the iterator (using * operator).
  • Close the file.

Let us see the code.

Output (from the file):

Hello!
We are Java2Blog.
You can a lot from our articles.
This method uses iterator.

Write Vector to File in C++ Using Range-Based for Loop

Range-based for loop is great when you have to traverse the contents of a container like vectors. You do not need to define the beginning and ending indices or iterators. The range-based for loop does it itself.

The result of each iteration i.e. each element is stored in the loop variable. You can write the loop variable directly to the file.

Let us see the code.

Output (from the file):

Hello!
We are Java2Blog.
You can a lot from our articles.
This method uses range-based for loop.

Write Vector to File in C++ Using the for_each() Function

The for_each() function applies a custom user function to all the elements of a container like a vector.

You can define a custom function that writes a number to a file stream and pass it to for_each(). In this way, you can write all elements to a file.

The definition of the for_each() function is given below.

Let us see the code.

Note that you shall define a global file stream to be used by the function that writes to the file.

Output (from the file):

Hello!
We are Java2Blog.
You can a lot from our articles.
This method uses for_each().

Write Vector to File in C++ Without Using a Loop

You can Write Vector to File without using a loop by calling the copy() function. The copy() writes all values of the vector to an output stream passed to it as an argument.

You can pass the file output stream as the output stream to which function writes the elements.

The definition of the copy() function is given below.

The function accepts a starting and an ending iterator to denote the range of values that it must write to the output stream. The third parameter is the output stream itself.

Note that you must create an output stream iterator to pass it to copy(). You can create an output stream iterator using the ostream_iterator class.

Let us see the code.

Output (from the file):

Hello!
We are Java2Blog.
You can a lot from our articles.
This method uses no loop.

Conclusion

The article discussed several different methods to Write Vector to File each with potential usage in different situations.

You can also use the write() function of the fstream class to write to the file. You can check out more about this method of writing files in our article here.

This is all about How to write Vector to file in C++.

Hope you enjoyed reading the article. Stay tuned for more articles. Happy Learning!

Was this post helpful?

Related Posts

  • 19 May

    Convert Vector to Array in C++

    Table of ContentsVectors and Arrays in C++Convert Vector to Arrays in C++Using the for loop to convert vector to array in C++Using the copy() function to convert vector to array in C++Using the transform() function to convert vector to array in C++Using the data() function to convert vector to array in C++Using the & operator […]

  • Remove element by value from vector in C++
    07 February

    Remove element by value in vector in C++

    Table of ContentsWhat are vectors in C++?Where can we remove an element from a vector?How to remove element by value in vector in C++?Using the std::find() function to remove element by value in vector in C++.Using the remove() function to remove element by value in vector in C++.Using the remove_copy() function to remove element by […]

  • Merge two vectors in C++
    11 June

    Merge two Vectors in C++

    Table of ContentsUsing merge() to merge two vectors in C++Using set_union() to merge two vectors in C++Using insert() to combine two vectors in C++Using move() & back_inserter to merge two vectors in C++Using copy() and back_inserter to concatenate two vectors in C++Using make_move_iterator() and insert() to merge two vectors in C++ConclusionWas this post helpful? vectors […]

  • Vector of Structs in C++
    18 May

    Vector of structs in C++

    Table of ContentsUsing Range ConstructorUsing Initializer listUsing custom constructorBonus ProgramConclusionWas this post helpful? In this article, we will see how to initialize and use vector of structs in C++. There are different ways to do it. This article will explain these methods clearly. Using Range Constructor We can utilize the range constructor for the initialization […]

  • Remove elements from vector in C++
    08 May

    How to remove element from a vector in C++

    How to remove elements from vector in C++

  • vector of vectors C++
    13 April

    How to create vector of vectors in C++

    Learn about how to create vector of vectors in C++.

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.