I'm writing a program that print an array to a text file, then read that array from that text file to another array, this is my program:
#include <iostream>
#include <fstream>
using namespace std;
void PrintToFile(int arr[], ofstream& PrintFile)
{
for (int i = 0; i < 10; i++)
{
arr[i] = i;
PrintFile << arr[i] << " ";
}
}
void ReadFromFile(int arr[], ifstream& ReadFile)
{
for (int i = 0; i < 10; i++)
ReadFile >> arr[i];
}
int main()
{
int arr1[10];
int arr2[10];
ofstream PrintFile("output.txt");
ifstream ReadFile("output.txt");
PrintToFile(arr1, PrintFile);
ReadFromFile(arr2, ReadFile);
for (int i = 0; i < 10; i++)
cout << arr1[i] << " ";
cout << endl;
for (int i = 0; i < 10; i++)
cout << arr2[i] << " ";
}
This is the output for the program:
0 1 2 3 4 5 6 7 8 9
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460
I don't understand why it printed out those number for the second array, it suppose to be the same as the first array, can someone help me with this? Thanks for your help!
ReadFile.is_open()isfalse.