int main() {
fstream file;
int array[4];
int x;
file.open("o.txt");
while (file>>x) {
cout << x << endl;
for (int i = 0; i < 4; i++) {
array[i] = x;
}
}
for (int i = 0; i < 4; i++) {
cout << array[i] <<' ';
}
file.close();
system("pause");
return 0;
}
Your array is too small to hold the entire contents of the file. You should also keep track of how many values have been read from the file - it may not be the same as the array size. (but cannot be greater).
int main()
{
// make size at least as large as needed
constint size = 20;
int array[size];
ifstream file("o.txt");
int count = 0;
int x;
// check that array is not already full
// and read integer from file,
while (count < size && file >> x)
array[count++] = x;
// display the values stored in the array
for (int i = 0; i < count; i++)
cout << array[i] <<' ';
}