Sign in to follow this  

searching file for variables

This topic is 4535 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok i am currently writing a phonebook program that is very basic in nature but at the moment i am stuck on a tricky problem. I just added an option for searching variables in the phone file but i can't fine out how i can search the file and make sure that the integral value specified by the user is correct. This is my phone file: Michael (333) 222-334 John (222) 111-156 So if i entered 222 i would want the program to find that value and make sure it is definatly there and if so display it. Is this possible to do? if so can someone pleas help me out on how to do this.

Share this post


Link to post
Share on other sites
The easiest thing to do is to define a phone book file format, such that you simply have to search specific fields. Using the address book on my cell phone as an example, each record in your file could contain First Name, Last Name, Company, Email, Home Number, Home Number 2, Work Number, Work Number 2, Mobile Number, Address (which has Street, City, State and ZIP+4 fields for US-only applications) and Notes fields. You specify a max number of characters for each of those fields as well as acceptable data types (numeric, alphabetical, alphanumeric) and then retrieving or searching for data becomes a matter of advancing through the appropriate fixed-size fields and testing for the presence of the search item.

As for the specifics of how to implement this, it will vary from language to language. We'll need more information to really help you, but you should give a shot at figuring it out for yourself.

Happy hacking.

Share this post


Link to post
Share on other sites
Sorry about that quote before i didn't relasize. Ok i am using C++ for my phone program and have overloaded the iostream << >> to output data to the file and screen but i can't search through the file at all. Like i can't think of any function that could find this value i want through the file, why is the STL so darn tricky for functions like that.

Share this post


Link to post
Share on other sites
Quote:
Original post by GameMasterXL
Quote:
Original post by orcfan32
Um, language?

Hmm why did you just spam the thread? i did not use any bad language in that thread so why post that?


Because he's simply making a query as to which language you're using. That to easier tailor forthcomming responses to that.
It's a good idea to always state clearly what (programming) language you're using when asking for help implementing stuff since it greatly increases the chance of getting relevant information.

Share this post


Link to post
Share on other sites
Well if you have stream input overloaded you could just use std::find with a custom predicate.

edit, it would look something like

ifstream in( "phonebook.txt");
istream_iterator<int> pos = find( istream_iterator<int>( in), istream_iterator<int>(), 42);
if( pos != istream_iterator<int>())
;//found to cool stuff


replace int with your phonebook entry class and add a predicate to find and you should be rolling along.

Share this post


Link to post
Share on other sites
cool thanks [smile], i also am finding it hard to find a good function for searching multidimensional arrays for strings aswell from the STL. Like for my parser i need to find array names and have no look upto now of finding a function on how to do that. By anychance do you know of a function that can seach a string from the given key in a multidimensional character array?

Share this post


Link to post
Share on other sites

This topic is 4535 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this