User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 456,589 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,591 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 501 | Replies: 3
Reply
Join Date: Oct 2007
Posts: 6
Reputation: Fredszky is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Fredszky Fredszky is offline Offline
Newbie Poster

Search function in textfile?

  #1  
Nov 4th, 2007
I spent 5-6 hours today to try to get a working searchfunction for my textfile,
can someone please put me in the right direction?

I have this textfile, its a register over contacts:

Number Contacts = 3*
James Brown:555-12345:james@brown.com
Jack Yellow:555-42345:jack@yellow.com
Sally White:555-12345:sally@yahoo.com

For example, i want to search for "yahoo" and find the sally string.
I cant put a code since i have noone working or close to it.
However, my contacts is in the memory like this:

contact[0]=James Brown:555-12345:james@brown.com
contact[1]=Jack Yellow:555-42345:jack@yellow.com

..and so on.

The closest solution ive tried is "memchr" to compare contact strings with a searchstring,
but cant get it to work, tried to understand it but i dont. Im new to programming.
Maybe its a bad sollution anyway..
Last edited by Fredszky : Nov 4th, 2007 at 1:57 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Search function in textfile?

  #2  
Nov 4th, 2007
The strstr() function #include <string.h> will return whether or not a given string contains a given substring.

  1. char string_to_search[] = "Hello, world!";
  2. char string_to_find[] = "world";
  3. char *pos;
  4. pos = strstr( string_to_search, string_to_find );
  5. if (pos != NULL)
  6. printf( "found it at index %d\n", (int)(pos -string_to_search) );
  7. else
  8. printf( "not found\n" );

Hope this helps.
Last edited by Duoas : Nov 4th, 2007 at 3:33 pm.
Reply With Quote  
Join Date: Oct 2007
Posts: 6
Reputation: Fredszky is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Fredszky Fredszky is offline Offline
Newbie Poster

Re: Search function in textfile?

  #3  
Nov 4th, 2007
I've started trying this function out, seems like i can use it!
I'll repost if (when) i get stuck for hours again ;-)

Thank you!
Last edited by Fredszky : Nov 4th, 2007 at 4:06 pm.
Reply With Quote  
Join Date: Oct 2007
Posts: 6
Reputation: Fredszky is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Fredszky Fredszky is offline Offline
Newbie Poster

Re: Search function in textfile?

  #4  
Nov 5th, 2007
Yes! Got it working

Thanks again
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 6:41 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC