How to Read and Write Arrays to/from Files in C++? Last Updated : 11 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, an array is a collection of elements of the same type placed in contiguous memory locations. A file is a container in a computer system for storing information. In this article, we will learn how to read and write arrays to/from files in C++. Example: Input: Array = {1, 2, 3, 4, 5}Output: // Write the array to a file, then read the array from the file Array: 1 2 3 4 5Reading and Writing Arrays to and from Files in C++To read and write arrays to and from files in C++, we can use the file streams from the standard library, std::ifstream for reading from files and std::ofstream for writing to files. We can read and write the arrays in a similar way as any other data. C++ Program to Read and Write Arrays to and from Files The below example demonstrates how to read and write arrays to/from files in C++. C++ // C++ program to demonstrate how to read and write arrays to/from files #include <fstream> #include <iostream> using namespace std; int main() { // Creating an array of size 5 int arr[] = { 1, 2, 3, 4, 5 }; int size = sizeof(arr) / sizeof(arr[0]); // Opening the file in write mode ofstream outfile("array.txt"); if (!outfile.is_open()) { cerr << "Failed to open file for writing.\n"; return 1; } // Writing the array elements to the file for (int i = 0; i < size; ++i) { outfile << arr[i] << " "; } // Closing the file outfile.close(); // Opening the file in read mode ifstream infile("array.txt"); // Reading the array elements from the file for (int i = 0; i < size; ++i) { infile >> arr[i]; } // Closing the file infile.close(); // Displaying the array elements cout << "Array elements: "; for (int i = 0; i < 5; ++i) { cout << arr[i] << " "; } cout << endl; return 0; } OutputArray elements: 1 2 3 4 5 Time Complexity: O(N), here N is the size of the array. Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Read and Write Arrays to/from Files in C++? R rohitpant4532 Follow Improve Article Tags : C++ Programs C++ cpp-array cpp-file-handling CPP Examples +1 More Practice Tags : CPP Similar Reads How to Read Data from a CSV File to a 2D Array in C++? A comma-separated value file also known as a CSV file is a file format generally used to store tabular data in plain text format separated by commas. In this article, we will learn how we can read data from a CSV file and store it in a 2D array in C++. Example: Input: CSV File = âdata.csvâ //contain 3 min read How to Read Binary Search Tree from File in C++? A binary search tree is a hierarchical data structure in which for every node in the tree, the value of all nodes in the left subtree is less than the node's value and the value of all nodes in the right subtree is greater than the node's value. This property of the binary search tree makes it effic 4 min read How to Read From a File in C++? Reading from a file means retrieving the data stored inside a file. C++ file handling allows us to read different files from our C programs. This data can be taken as input and stored in the program for processing. Generally, files can be classified in two types:Text File: Files that contains data i 4 min read How to Initialize Array With Values Read from a Text File in C++? In C++, arrays store a fixed-size collection of elements of the same type. We can initialize these elements with any valid value. In this article, we will learn to initialize a C++ array with values read from a text file. Example: Input:Text File = âarray.txtâ // contains 1 2 3 4 5Output:// Read the 2 min read How to Redirect cin and cout to Files in C++? In C++, we often ask for user input and read that input using the cin command and for displaying output on the screen we use the cout command. But we can also redirect the input and output of the cin and cout to the file stream of our choice. In this article, we will look at how to redirect the cin 2 min read Like