Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

NeedHelp c++ code to open a file and search a token (Unix environment)

jedi06jedi06 Member Posts: 38
[b][red]This message was edited by jedi06 at 2006-1-30 21:50:15[/red][/b][hr]
[b][red]This message was edited by jedi06 at 2006-1-30 21:49:0[/red][/b][hr]
What program does (Compiled and run on Unix):

Get Key and filename from user
1.) Open the file.
2.) Read each line of the file. If the key matches the first token of the line, display the line.

Here is the link to the program assignment(cs216)http://www.cs.uky.edu/~paulp/CS216PGM1.html
This is supposedly an easy assignment. Just need help with the c++ program, Shell script is done. Can anyone help please?




«1

Comments

  • stoberstober Member Posts: 9,765 ✭✭✭
    : What program does (Compiled and run on Unix):
    :
    : Get Key and filename from user
    : 1.) Open the file.
    : 2.) Read each line of the file. If the key matches the first token of the line, display the line.
    :
    : Here is the link to the program assignment(cs216)http://www.cs.uky.edu/~paulp/CS216PGM1.html
    : This is supposedly an easy assignment. Just need help with the c++ program, Shell script is done. Can anyone help please?
    :

    [blue]yes, it is an easy assignment. post your best efforts at solving the problem[/blue]

  • jedi06jedi06 Member Posts: 38
    #include
    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.

    : [blue]yes, it is an easy assignment. post your best efforts at solving the problem[/blue]
    :
    :

  • stoberstober Member Posts: 9,765 ✭✭✭
    [b][red]This message was edited by stober at 2006-1-31 12:29:13[/red][/b][hr]
    : #include
    : 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.
    :
    : : [blue]yes, it is an easy assignment. post your best efforts at solving the problem[/blue]
    : :
    : :
    :
    :
    [blue]
    1. include header file
    2. create an ifstream object
    3. create a loop to read the file until end-of-file is reached.
    [code]
    #include

    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
    }
    }
    [/code]





  • jedi06jedi06 Member Posts: 38
    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 functions or a good c++ book any suggestions?

    : [b][red]This message was edited by stober at 2006-1-31 12:29:13[/red][/b][hr]
    : : #include
    : : 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.
    : :
    : : : [blue]yes, it is an easy assignment. post your best efforts at solving the problem[/blue]
    : : :
    : : :
    : :
    : :
    : [blue]
    : 1. include header file
    : 2. create an ifstream object
    : 3. create a loop to read the file until end-of-file is reached.
    : [code]
    : #include
    :
    : 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
    : }
    : }
    : [/code]
    :
    :
    :
    :
    :
    :

  • stoberstober Member Posts: 9,765 ✭✭✭
    There are lots of them on the net -- use google and you will find them. Here is one

    http://www.cplusplus.com/ref/iostream/fstream/

    Not really a tutorial, but explains all the functions.
  • jedi06jedi06 Member Posts: 38
    So, I got one more question.

    When I try to open a file does it automatically know where the file is located?

    If I had to guess the program has to be executed in the same directory of the file to open?

    Also, I assume it does not matter the extension of the file becuase it just looks at the binary representation of the file and uses ascii representation to display it to user?

    I just need a crash course by next Wednesday. :)
  • stoberstober Member Posts: 9,765 ✭✭✭
    : So, I got one more question.
    :
    : When I try to open a file does it automatically know where the file is located?

    [blue]NO. If the file is not in the current working directory when you have to add the full path to the filename, such as "c:\Program Files\SomeDirectory\myfile.txt". Or use relative path, such as "..SomeDirecotry\myfile.txt"[/blue]
    :
    : If I had to guess the program has to be executed in the same directory of the file to open?
    [blue]The executable does not have to be in the same directory as the file -- see my above comment[/blue]
    :
    : Also, I assume it does not matter the extension of the file becuase it just looks at the binary representation of the file and uses ascii representation to display it to user?
    :
    [blue]The extension is not really relevant. But it is customary to use standard extensions, such as .txt for text file and .dat for some binary database files. [/blue]
  • jedi06jedi06 Member Posts: 38
    [b][red]This message was edited by jedi06 at 2006-2-4 12:54:1[/red][/b][hr]
    Ok I got a working version. I found a bug also and I want to fix it. When I open a file and its done, if i try to open the same file again i don't get any outputs the second time. Like it only works one time per file. I don't like that, but it does work fine other than that. Any suggestions/modifications/simplifications? This is my first program in a couple years and I knew nothing about fstream. Sorry about the comments this isn't very good for posting code need more room...

    #include
    #include
    #include
    using namespace std;

    int main(){

    string filename;
    string key;
    ifstream readFile;
    string token;
    string line;

    do{
    cout<<"Enter the file to read (or enter exit to end the program): ";
    cin>>filename;
    if(filename!="exit")
    {
    readFile.open(filename.c_str()); // open the file
    while(!readFile.is_open() && filename!="exit")
    {
    cout<<"Open for file "<<filename<<" failed. Enter another file "
    <<"name or exit to end the program: ";
    cin>>filename;
    readFile.open(filename.c_str());
    }
    if(filename!="exit")
    {
    cout<<"Enter a key: ";
    cin>>key;

    while(readFile){
    readFile>>token;

    getline(readFile,line);
    if(token==key)
    cout<<key<<line<<endl;
    }


    readFile.close();
    }else;
    }else;
    }while(filename!="exit");
    return 0;
    }


  • stoberstober Member Posts: 9,765 ✭✭✭
    after closing the file, you need to call readfile.clear() to clear the eof and other flags.
  • jedi06jedi06 Member Posts: 38
    Found one more bug. Why does it output the last line twice, only does it for the last line?

    The key is 23.
    The file contains:
    142 Hello
    55 No
    12 Noo
    ad NOOO
    142 World
    23 sdfas
    23 sdf

    This is what it outputs:
    23 sdfas
    23 sdf
    23 sdf

    Here is the code:
    while(readFile){
    readFile>>token;

    getline(readFile,line,'
    ');
    if(token==key)
    cout<<key<<line<<endl;
    }

«1
Sign In or Register to comment.