Sunday, July 12, 2009

C++ Help! How can I modify my program!?

I have to rewrite my program so that it prompts the user to enter the name of the file to be read, instead of reading the source file itself. This is my program:





#include %26lt;fstream%26gt;


#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


using namespace std;





int main()


{


ifstream fin;


fin.open("readFile.cpp");


if (!fin.good()) throw "I/O error";





while (true)


{


if (!fin.good()) break;





string lineFromFile;


getline(fin, lineFromFile);


cout %26lt;%26lt; lineFromFile %26lt;%26lt; endl;


} // while


fin.close();





return 0;


} // main

C++ Help! How can I modify my program!?
Make a string, called String someString. prompt the user for that string using getline instead of cin because cin ignores whitespace and you might encounter a space in the file name. Replace fin.open("readFile.cpp") with fin.open(someString).


No comments:

Post a Comment