All pastes #10327 Raw Edit

kormoc

public cpp v1 · immutable
#10327 ·published 2005-04-26 06:58 UTC
rendered paste body
#include <iostream>#include <fstream>using namespace std;int countfile(char *filename);double *makearray(char *filename);int main(void){ char *filename="doubles1.txt"; double *array=makearray(filename); for(int i=0;i<countfile(filename);i++)  cout << "array[" << i << "]=" << array[i] << endl;}int countfile(char *filename){ fstream filestr; int count=0; double d; filestr.open(filename, ios::in); while(!filestr.eof()) // Yes harv, this fixes the count being one off error  filestr >> d && count++; // cause the read will return false and not incriment filestr.close(); return count; // and thus count is correct}double *makearray(char *filename){ fstream filestr; int i=0; double *dblArr = new double[countfile(filename)]; filestr.open(filename, ios::in); while(!filestr.eof())  filestr >> dblArr[i++]; filestr.close(); return dblArr;}