Call file.eof() to check if you've hit end-of-file. It returns true only after a read fails due to reaching the end. Checking before reading gives false, you must attempt a read first.
Better pattern: check the read operation itself. while(file >> data) loops until reads fail. This handles both EOF and read errors. Using while(!file.eof()) leads to processing the last value twice.
After hitting EOF, the stream enters an error state. You must call file.clear() before seeking backward and reading again. Otherwise, all subsequent reads fail silently.