searching file for variables

Started by
8 comments, last by DigitalDelusion 18 years, 9 months ago
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.
Advertisement
Um, language?
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit
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.
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.
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.
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
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.
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
cool thanks, what does an iterator do? is it like looping?
Quote:Original post by GameMasterXL
cool thanks, what does an iterator do? is it like looping?

conceptually iteraters closly resemble your everyday pointer, here's a clicky IteratorPattern

HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
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?
for the parser it really sounds like you ought to use an associative container like std::map.
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats

This topic is closed to new replies.

Advertisement