Thanks. This was something I guess I was already supposed to know from cs115,215 but apparently professor decided to skip over that. I need some good documentation on #include <fstream> functions or a good c++ book any suggestions?
:
This message was edited by stober at 2006-1-31 12:29:13
: : #include <iostream>
: : using namespace std;
: :
: : int main(){
: : string filename;
: : string key;
: :
: : do{
: : cout<<"Enter the file to read (or enter exit to end the program):";
: : cin>>filename;
: : if(filename!="exit"){
: : cout<<"Enter a key:";
: : cin>>key;
: :
: : //OPEN filename;
: : //SEARCH key; //Only first token of the line in (filename)
: : //if(token==key)
: : //DISPLAY LINE;
: : }
: : else
: : ;
: :
: : }while(input!="exit");
: : }
: :
: : lol... I have no idea on how to open and search a file and print lines of that file. I was thinking maybe I'm supposed to pass commands to the operating system.
: :
: : :
yes, it is an easy assignment. post your best efforts at solving the problem
: : :
: : :
: :
: :
:
: 1. include <fstream> header file
: 2. create an ifstream object
: 3. create a loop to read the file until end-of-file is reached.
:
: #include <fstream>
:
: int main()
: {
: ...
: // open the file
: ifstream in(filename.c_str());
: // make sure the file open was successful
: if( !in.is_open() )
: {
: cout << "Error" << endl;
: return 1;
: }
: // read each line
: string line;
: // getline() will return 0 value on end-of-file or on error
: while( getline(in,line) )
: {
: // line now contains a line from the file.
: // do something with it
: }
: }
:
:
:
:
:
:
: