Thread: File Handlingness

  1. #1
    Disrupting the universe Mad_guy's Avatar
    Join Date
    Jun 2005
    Posts
    258

    File Handlingness

    Well I just decided to code a basic IRC bot that is a complete ripoff of the bot on the irc.efnet.net #winprog IRC channel.

    Essentially I want the bot to have the ability to give information over an API call by someone simply doing,

    !api CreateFile

    In the channel and it will answer and such. Unfortunately, while testing out my file handling function in another source file I've hit a snag, spaces in the files' buffer cause it to truncate the string.

    My files are laid out like this:

    APICALLNAME :: DESCRIPTION (this isn't longer than 1024 bytes)

    So if I have like, on a totally unrelated note this:

    ASDF :: Fight Club

    It will give back "Fight" and that's all.



    Function (excuse the stupid name of the function):
    Code:
    void sidiousSearch(srctSearch *information) {
    	//Variables
    	char  lib,											  //This is a char to specify the file to open
    	      rightFile[10],								  //File to open
    	      line[1024],									  //Line from file (description)
    	      nameOfAPI[40];								  //The name you're searching for
    	FILE  *file2Open;									  //File handle
    	int   sent = 1;                                       //Assume failure
    	char  errorMSG[] = "Sorry, I couldn't find that API"; //Incase it wasn't found
    
    
    	//Make sure we have the appropriate file.
    	lib  = information->query[0];
    	sprintf(rightFile,"file[%c].txt",lib);
    
    
    	//Enter exception handling block and ........
    	_try {
    		//Open
    		file2Open = fopen(buffer,"r");
    		if(file == NULL)
    			_leave;
    
    		//Enter loop
    		while((fscanf(file,"%s :: %s",nameOfAPI,line)) != EOF) {
    			if(strcmp(nameOfAPI,information->query)==0) {
    				//Success
    				send(information->s,line,strlen(line),0);
    				sent = 0;
    				break;
    			}
    		}
    	}
    	_finally {
    		//Clean up file
    		if(file2Open != NULL)
    			fclose(file2Open);
    	}
    
    	if(sent == 1)
    		send(information->s,errorMSG,strlen(errorMSG),0);
    
    	WaitForSingleObject(hMutex4Thrs,INFINITE);
    	thrNumInUse--;
    	ReleaseMutex(hMutex4Thrs);
    }
    The srctSearch struct has a socket to communicate to (the IRC servers') and then it has the query to be searched for, I decided to split up the files in an order so that depending on the first letter of the APIs' name it will open a specific file to that (as to avoid just searching through one massive table.)


    I am wondering if there is a way to get around this with file calls like this or if I would have to put Win32 file handling at play.



    Also, does anybody know of a string comparison function that does INcase sensitive matches? If the file has the API name "ASDF" and someone would go "!api Asdf" it would not work. Or do I have to make one myself?
    Last edited by Mad_guy; 07-23-2005 at 12:14 AM.

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by Mad_guy
    Also, does anybody know of a string comparison function that does INcase sensitive matches? If the file has the API name "ASDF" and someone would go "!api Asdf" it would not work. Or do I have to make one myself?
    For Windows, look into the stricmp() and strnicmp() functions. Or, if you are using a *nix machine, it would be strcasecmp() and strncasecmp(). These are case insensitive versions of strcmp(), and strncmp().

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Read the FAQ on getting a line from the user to see how to fix your space issue. (Hint: Plain old %s stops on whitespace.)


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  2. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM