C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
NeedHelp c++ code to open a file and search a token (Unix environment) Posted by jedi06 on 30 Jan 2006 at 9:47 PM
This message was edited by jedi06 at 2006-1-30 21:50:15

This message was edited by jedi06 at 2006-1-30 21:49:0

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?




Report
Re: NeedHelp c++ code to open a file and search a token (Unix environm Posted by stober on 31 Jan 2006 at 4:27 AM
: 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?
:

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

Report
Re: NeedHelp c++ code to open a file and search a token (Unix environm Posted by jedi06 on 31 Jan 2006 at 10:01 AM
#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
:
:

Report
Re: NeedHelp c++ code to open a file and search a token (Unix environm Posted by stober on 31 Jan 2006 at 12:26 PM
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
   }
}






Report
Re: NeedHelp c++ code to open a file and search a token (Unix environm Posted by jedi06 on 1 Feb 2006 at 8:09 AM
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
:    }
: }
: 

:
:
:
:
:
:

Report
Re: NeedHelp c++ code to open a file and search a token (Unix environm Posted by stober on 1 Feb 2006 at 12:01 PM
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.
Report
Re: NeedHelp c++ code to open a file and search a token (Unix environm Posted by jedi06 on 1 Feb 2006 at 2:28 PM
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. :)
Report
Re: NeedHelp c++ code to open a file and search a token (Unix environm Posted by stober on 1 Feb 2006 at 9:49 PM
: So, I got one more question.
:
: When I try to open a file does it automatically know where the file is located?

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"
:
: If I had to guess the program has to be executed in the same directory of the file to open?
The executable does not have to be in the same directory as the file -- see my above comment
:
: 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?
:
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.
Report
Re: NeedHelp c++ code to open a file and search a token (Unix environm Posted by jedi06 on 4 Feb 2006 at 11:45 AM
This message was edited by jedi06 at 2006-2-4 12:54:1

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 <iostream>
#include <fstream>
#include <string>
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;
}


Report
Re: NeedHelp c++ code to open a file and search a token (Unix environm Posted by stober on 4 Feb 2006 at 12:59 PM
after closing the file, you need to call readfile.clear() to clear the eof and other flags.
Report
Re: NeedHelp c++ code to open a file and search a token (Unix environm Posted by jedi06 on 4 Feb 2006 at 6:12 PM
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,'\n');
if(token==key)
cout<<key<<line<<endl;
}

Report
reply to the last Posted by Donotalo on 4 Feb 2006 at 8:47 PM
: 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,'\n');
: if(token==key)
: cout<<key<<line<<endl;
: }
:
:

try the following approach:
while(!readFile.eof()){
:                   readFile>>token;
:                   
:                      getline(readFile,line,'\n');
:                      if(token==key)
:                         cout<<key<<line<<endl;
:                   }
: 



~Donotalo()

Report
Re: reply to the last Posted by jedi06 on 5 Feb 2006 at 9:20 AM
This message was edited by jedi06 at 2006-2-5 9:37:24

This message was edited by jedi06 at 2006-2-5 9:21:1

Nope (readFile.eof()) didn't work exactly the same as before (output last line twice), but I did find a solution below. I have no idea why it works, anyone know why?

Basically I have to check if the end of file has been reached again inside the loop. Seems to me that once the last line has been read it should stop the loop. Must be that it doesnt know it is the end until there are no more lines, so it must go into one more loop and it tries to take data from the file but there is no data so it says this must be the end of file. Thanks for all the help. Cheers.

:
: :
:
: while(readFile){
: :                   readFile>>token;
: :                   getline(readFile,line,'\n');
: :                      if(readFile) //checking again
: :                         if(token==key)
: :                            cout<<key<<line<<endl;
: :                   }
: : 
: 

:









 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.