Thread: Search a file for a value by key

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    14

    Search a file for a value by key

    Ok, so I am trying to retrieve a value from a file given a search key. The file will be a php file containing defines, like this:
    Code:
    define("LOG_DEBUG_LEVEL", "0");
    This is what I am doing, which find the value, but wont error if its not found. If I comment out the above define in the file, no error is logged.

    Code:
    int getDebugLevel( char *key )
    {
    
        FILE *fp;
        char *temp;
        char *strLvl;
        char line[256];
    
        fp = fopen(MYFILE, "r");
        if ( fp == NULL )
        {
            syslog(LOG_ERR,"Unable to open %s for reading", MYFILE);
            return 1;
        }
    
        while(fgets(line, sizeof(line), fp) != NULL)
        {
            if(strstr(line, key))
            {
                temp = strdup(line);
                strLvl = strsep(&temp, "\"");
                strLvl = strsep(&temp, "\"");
                break;
            }
        }
    
        fclose(fp);
    
        if( strLvl == NULL )
        {
            syslog(LOG_ERR,"Unable to find DEBUG LEVEL in %s" MYFILE);
            return 1;
        }
    
        strncpy(value, strLvl, strlen(strLvl));
        value = atoi(value);
    
        return 1;
    }

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    14
    ok, n/m I just saw the return 1;

    ** That wasn't it, still not working **
    Last edited by fredanthony; 01-16-2007 at 03:59 PM.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I have changed only the part where you check that the line contains the key and retreives the value (made it a function)
    Code:
    int getValue( const char *line, const char*key, int*pValue )
    {
    	const char* p = strstr(line, key);
    	if(!p)
    		return -1;//not found
    
    	p += strlen(key);
    	if(strncmp(p, "\", \"",4)==0)
    	{
    		p += 4;
    		*pValue = atoi(p);
    		return 0; //OK
    	}
    	return -2; //wrong string format
    }
    PS. This function should be called like this:
    int value;
    int ret = getValue(line, "LOG_DEBUG_LEVEL",&value);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM