Thread: Search in a file for some data ???

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    64

    Question Search in a file for some data ???

    What is the best way to search for some data in a file.

    I have used:


    if(seekfile)
    {
    while(fgets(DataBuffer,(int)sizeof DataBuffer, seekfile) != NULL)
    {
    sscanf(MobilDataBuffer,"%d %d",tempone, temptwo);
    if(num== tempone)
    {
    *answer = OK;
    return 0;
    }
    }
    }
    return *answer;

    I need to find a number in the file, and when I find it, It shall send back a yes or no, if the number is found in the file.

    What i the best way to do this..
    Tx hope to get some help...

    Gugge


    Ps: And by the way, how do I turn on the thing, so when I'm writing code, it looks fine istead of this #¤"¤¤#.....??
    !G!

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >What is the best way to search for some data in a file.

    That depends on how the numbers in your file are stored. If the numbers are stored in a random order, then you should start at the beginning of the file and reach each number until you found it or until the end of file is reached. They way you do, seems good to me.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here's a couple of ways to do it.

    This first way is looking for numbers specifically.
    Code:
    #include <stdio.h>
    
    #define SEARCH_FOR_NUM 10
    
    int main(void)
    {
    	FILE *fp;
    	int i, j;
    	
    	if ((fp = fopen ("test.txt", "r")) == NULL)
    	{
    		perror ("test.txt");
    		return 1;
    	}
    	
    	while ((j = fscanf(fp, "%d", &i)) != EOF)
    	{
    		if (j != 1) 
    		{
    			fgetc(fp); 
    			continue;
    		}
    		
    		if (i == SEARCH_FOR_NUM) printf ("Found it!\n");
    	}
    	
    	fclose (fp);
    	
    	return 0;
    }
    Or you could look for strings this way:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define SEARCH_FOR_STR "10"
    
    int main(void)
    {
    	FILE *fp;
    	char buf[1024]; /* choose appropriate size */
    	
    	if ((fp = fopen ("test.txt", "r")) == NULL)
    	{
    		perror ("test.txt");
    		return 1;
    	}
    	
    	while (fgets(buf, 1024, fp))
    	{
    		if (strstr(buf, SEARCH_FOR_STR))
    			printf ("Found it!\n");
    	}
    	
    	fclose (fp);
    	
    	return 0;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-26-2008, 02:12 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Writing and modifying data in a file
    By Micko in forum C Programming
    Replies: 2
    Last Post: 02-17-2005, 03:42 AM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM