#include <iostream>
#include <fstream>
using namespace std;
/*
* don't worry about declaring the variables if you don't intend to use them.
*/int main()
{
int array[100];
fstream file ("test.dat",std::ios::in);
/*
* I'd do something like this so that you don't have to wrap all
* your code in an if statement
*/if(!file.is_open())
{
std::cout<<"Unable to open file!"<<std::endl;
exit(1);
}
int x=0;
/*
* you should generally use a read to control a loop - check the FAQ
* to find out why
*/while(file>>array[x])
{
/*
* your main problem was here - you were outputting the pointer
* to the first element of the array over and over again.
* instead of
* cout<<*array<<endl;
* you should have
*/
cout<<array[x]<<endl;
x++;
}
file.close();
/*
* you had an output here that would have tried accessing uninitialized
* data if I had left it in and modified it to work.
*//*
* avoid using system() whenever you can - it makes your code non-
* portable, and much worse, opens up a huge security vulnerability
*/
std::cout<<"Press ENTER to Continue..."<<std::endl;
std::cin.get();
/*
* also, avoid using macros if possible. Return '0' instead of
* EXIT_SUCCESS
*/return 0;
}

Last edited by major_small; 01-07-2006 at 04:43 AM.
Reason: cleaning up after syntax highlighter

As long as we're cleaning up the code a little, I'd add one more check. This:

Code:

while(file>>array[x])

Should maybe be this:

Code:

while(x < 100 && file>>array[x])

This ensures that we don't accidentally try to read in too much data (more than the array can handle).

"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens