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?

Leave a Reply

Your email address will not be published. Required fields are marked *