#include #include #include using namespace std; //--------------------------- convertString ---------------------------------- // Convert a string object into a character array (actually char*) char* convertString(string s) { int length = s.length(); // allocate memory one longer than length to store null character // which is required at the end of char arrays so they display correctly char* charString = new char[length+1]; s.copy(charString, length, 0); charString[length] = '\0'; // put null char at array end return charString; } //---------------------------------- main ------------------------------------ // Allow user to enter a filename from standard input int main() { string s; cout << "please enter filename: "; cin >> s; char* filename = convertString(s); ifstream infile(filename); // constructor opens file if (!infile) { cerr << "File could not be opened." << endl; return 1; } // simple example of use to read ints until eof and display int n; for (;;) { infile >> n; if (infile.eof()) break; cout << n << endl; } delete [] filename; // clean up memory filename = NULL; return 0; }