自从大学学完C之后,就再也没用过它了,
在网上找代码,七拼八凑之后,终于成形~~勉强能用,不喜勿喷,^_^!
int GetValue(const wchar_t *key, wchar_t *value) { FILE* fpcfg = NULL; wchar_t var[256], val[256];//key,value wchar_t linebuf[1024]; wchar_t* ptr1 = NULL; wchar_t* ptr2 = NULL; wchar_t* delimiter = NULL; int k = 0; assert(key != NULL && value != NULL); //testConfig.ini中的内容为: //key1 = value1 //key2 = value2 fpcfg = fopen("testConfig.ini", "rt"); if(fpcfg == NULL) { return -1; } while(fgetws(linebuf, sizeof(linebuf), fpcfg) != NULL) { //ignore annotation line if(linebuf[0]=='#' || linebuf[0]==';') { continue; } //ignore empty line ptr1 = linebuf; while(*ptr1==0x20 || *ptr1==' ') ptr1++; if(!*ptr1 || *ptr1==' ') { continue; } //search "=" ptr2 = ptr1; while(*ptr2 && *ptr2!='=') { ptr2++; } delimiter = *ptr2?ptr2:NULL; //if current line start with "=", continue next line if(!delimiter || delimiter==ptr1) { continue; } //*delimiter = ' '; ptr2 = delimiter-1; //ignore blank before "=" while(*ptr2==0x20 || *ptr2==' ') { ptr2--;} //check key length k = ptr2-ptr1+1; if(k>(int)sizeof(var)-1) { //The key name is out of length." continue; } //save key name ptr2 = var; while(k--) { *ptr2++ = *ptr1++; } *ptr2 = '