Searching a string

This is a discussion on Searching a string within the C Programming forums, part of the General Programming Boards category; So i'm stumped. I've got a file that looks like this. Code: #include scripts/extra/loot_creatures_BHN.scp #include scripts/extra/loot_creatures_aq.scp #include scripts/extra/loot_creatures_revelers.scp #include scripts/extra/loot_creatures_elites.scp ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    17

    Searching a string

    So i'm stumped. I've got a file that looks like this.

    Code:
    #include scripts/extra/loot_creatures_BHN.scp
    #include scripts/extra/loot_creatures_aq.scp
    #include scripts/extra/loot_creatures_revelers.scp
    #include scripts/extra/loot_creatures_elites.scp
    
    [loottemplate 3]
    loot=2794 100.000
    loot=884 100.000
    loot=1129 100.000
    loot=2059 0.013
    loot=2194 0.007
    loot=12998 0.006
    And I want to search the string until after the space, then grab those numbers. I'm not entirely sure how to do it. I thought of maybe using sizeof to determine the length of it, but still i've got no clue. any ideas?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while( fgets( buf, BUFSIZ, fp ) )
    {
        if( ( ptr = strchr( buf, ' ' ) ) && *( ptr + 1 ) )
        {
            ptr++;
            /* you are just past the first space in the buffer, do something... */
        }
        else
        {
            /* no spaces, or nothing after the space, do whatever... */
        }
    }
    Look in your book, man page, etc, of choice if you're not familiar with either of the functions.

    Also, strlen returns the length of a string. sizeof does not unless you're very very lucky.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Or one for the sscanf junkie...
    Code:
    /* ... */
          char line[BUFSIZ];
          double value;
          while ( fgets(line, sizeof line, file) != NULL )
          {
             if ( sscanf(line, "loot=%*s %lf", &value) == 1 )
             {
                printf("value = %g\n", value);
             }
          }
    /* ... */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    17
    Thanks guys. Not all of it makes sense, especially the scanf stuff, but i'll figure it out. Thanks again!

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    17
    Alrighty. So this is what I came up with. I haven't tried compiling it yet, because i'm still sketchy on writing it back to the file, so I wanted to pass it by you guys for inspection before I bothered. Again, any help is always appreciated!

    Code:
    void handle_file( int modifier, int max_allowed, int add_to )
    {
    	int x, par;
    
    	for ( x = 0; x < loot_count; x++ )
    	{
    		if ( ( modifier + add_to ) < max_allowed )
    		{
    			if ( modifier > max_allowed )
    			{
    				modifier = max_allowed;
    			}else{
    			modifier + add_to;
    
    			}
    		}
    	}
    
    	return modifier;
    }
    
    int main ( void )
    {
    	int max_allowed, add_to, x, modifier;
    	char filename[50];
    	char buf[1000];
    	char * ptr;
    
    
    	printf("Enter the filename to be used.\n");
    	scanf( "%s", filename );
    	printf("Enter the max allowed loot result.\n");
    	scanf( "%d", &max_allowed );
    	printf("Enter the number to add to the loot.\n");
    	scanf( "5d", &add_to );
    	printf("Processing...\n");
    
    	FILE * fp;
    
    
    	if ( fp = fopen( filename, "r" ) = NULL )
    	{
    		printf("Invalid file name.\n");
    		exit(1);
    	}
    
    	while( fgets( buf, BUFSIZ, fp ) )
    	{
    		if( ( ptr = strchr( buf, ' ' ) ) && *( ptr + 1 ) )
    		{
    		    ptr++;
    		    fscanf( buf, "%d" &modifier );
    		    handle_file( modifier, max_allowed, add_to );
    		    fprintf( buf, "%d", &modifier );
    	   	}else{
    			printf("Completed.\n");
    			exit(1);
    		}
    	}
    
    	return 0;
    }

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    First, posting expected input and output may be better than not-yet-compileable code; it is certainly easier to debug an example than your unseen intentions.

    Now I get a bit nit-picky.

    Code:
    	scanf( "%s", filename );
    User Input: Strings and Numbers [C]

    -----

    In the code,
    Code:
    	char buf[1000];
    	char * ptr;
    	/* ... */
    	FILE * fp;
    	if ( fp = fopen( filename, "r" ) = NULL )
    	/* ... */
    	while( fgets( buf, BUFSIZ, fp ) )
    	{
    		if( ( ptr = strchr( buf, ' ' ) ) && *( ptr + 1 ) )
    		{
    		    ptr++;
    		    fscanf( buf, "%d" &modifier );
    		    handle_file( modifier, max_allowed, add_to );
    		    fprintf( buf, "%d", &modifier );
    	/* ... */
    You need to look up the proper usage of the functions' parameters. And I wouldn't recommend mixing fgets with fscanf. If you open a file as read-only, how should you expect to write to it? And this gets us to muddier areas of modifiying a file in-place -- which brings me back to the start: what do you expect for input and output?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,489
    Also, be honest about your buffer sizes otherwise fgets() is little better than gets()

    > char buf[1000];
    > fgets( buf, BUFSIZ, fp )

    Either do
    char buf[BUFSIZ];

    Or
    fgets( buf, sizeof buf, fp )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    17
    Well the idea is I want to grab the number, then run it through the modifier, then overwrite the number I just got. I've done similar things by just clearing the entire thing. I'll use your examples though and see if I can knock something out. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  2. Need help on String Searching.
    By RP319 in forum C++ Programming
    Replies: 3
    Last Post: 11-21-2005, 09:33 PM
  3. Visual C++ 2005 linking and file sizes
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2005, 09:41 PM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21