Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > help with string search

Thread Tools

help with string search

Posts: n/a

B. Williams wrote in message ...
>> // ------------------------------------
>> Post your code(attempt), what you expect and what you get (+errors (first
>> three, if many)).
>> Bob R POVrookie

>Sorry I'm just getting around to posting this. My tutor had me start over.


>The search function I was asking for assistance with is posted below.
>void updatePower( string filename ) {

// > ifstream in(filename.c_str(), ios::in);

The '(std:ios::in' is not needed. It is the default for an 'std::ifstream'.

ifstream in( filename.c_str() );

> int cnt = 0;
> // read all records into database until we reach the end of the file
> while (!in.eof()) {

Using the 'eof()' as a condition of the while is bad practice. By the time
'eof()' goes true is too late and you'll already have read past the end. It
will loop one too many.

> database[cnt++].input(in);

I don't have a clue what 'database' is.
Without knowing what 'input()' does, I can't advise on how to do your while
Common use is:

std::string line;
while( std::getline( in, line) ){ // read a whole line from ifstream 'in'.
// do something with 'line'

> }
> // close text file
> in.close();

The 'close()' does not reset the flags. It won't matter here, but, if you
ever need to re-use the object, do the following too:


> cout << "Enter entry ID to change balance:";
> cin >> id;

I don't know what 'id' is. It's not in this scope.

> if ( id < 0 || id > 49 ) {
> cout << "illegal ID - abort." << endl;
> return;
> }
> // enter new power
> double power;
> cout << "Enter new power:";
> cin >> power;
> // set new customer balance
> database[ id ].setupdatePower( power );
> // write customers back to text file

// > ofstream ofs(filename.c_str(), ios:ut);

The '(std:ios:ut' is not needed. It is the default for an
Are you aware that you are writeing out to the same file you just read in,
with the same data?
Since 'filename' is a non-const std::string, you could do:

filename = "new" + filename; // that prepends "new" to the name.

ofstream ofs( filename.c_str() );

> for( int i=0; i < cnt-1; ++i )
> database[i].output(ofs); // update, ie reset your file pointer
> ofs.close();

The 'close()' is not needed since the closing brace will delete the ofs
object. (but, IMHO, it is not a bad habit (won't hurt anything)).

> }

Keep at it.
Bob R

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help understand probems - Binary Search and Sequenital Search Timmy C++ 5 07-09-2007 02:41 PM
How to search for literal string in Windows Desktop Search? Computer Support 0 02-06-2007 04:58 AM
search within a search within a search - looking for better script times out Abby Lee ASP General 5 08-02-2004 04:01 PM
String search vs regexp search Anand Pillai Python 10 10-15-2003 08:21 AM