Monday, May 24, 2010

Read text from Console in C++ ?

Please i need the code to read a text any text from the console.


reading as string or pointer of array.


for example how can i read "Hello"


notice that user can enter any text so the could should read any string text the user entered....even i don't know what tayp should i use

Read text from Console in C++ ?
// reading a text file


#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;


#include %26lt;string%26gt;


using namespace std;





int main () {


string line;


ifstream myfile ("the path of the .txt file");


if (myfile.is_open())


{


while (! myfile.eof() )


{


getline (myfile,line);


cout %26lt;%26lt; line %26lt;%26lt; endl;


}


myfile.close();


}





else cout %26lt;%26lt; "Unable to open file";





return 0;


}

strawberry

No comments:

Post a Comment