12345678910111213141516171819202122232425262728293031323334353637
#include <iostream> #include <fstream> #include <vector> using namespace std; int ReadFile(const std::string & fileName, vector<int> & output) { int intBuffer; int numIntsRead = 0; ifstream inFile(fileName.c_str()); while(!inFile.eof()) { inFile >> intBuffer; // stream ok? if(!inFile.fail()) { ++numIntsRead; output.push_back(intBuffer); } else break; // probably redundant considering while condition } return numIntsRead; // or use this and eliminate the local "numIntsRead" return output.size(); } int main(void) { string myFile = "C:\\Temp\\numbers.txt"; vector<int> myFilesInts; int myFilesIntsCount = ReadFile(myFile, myFilesInts); cout << "Read " << myFilesIntsCount << " integers from file." << endl; }