![]() |
VOOZH | about |
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 5
Output:
// Read the values from the file and initialize the array
Array: 1 2 3 4 5
To initialize a C++ array with values read from a text file, we can use the std::ifstream for reading from files in a similar way as any other data and then initialize the array using that data.
The below program demonstrates how we can initialize a C++ array with values read from a text file.
Array Elements: 1 2 3 4 5Time Complexity: O(K), here K is the number of array elements.
Auxiliary Space: O(1)