0
設定ファイルの解析用のライブラリがありますが、独自の実装を作成しようとしています。問題は、configオプションを見つけることができますが、私が探しているものと比較しようとするとdelimeterが失敗する前に文字列を比較することです。 Testという単語の前後に文字があるかどうかを調べることができないため、私のプログラムではTest2やTest3のようなことが可能なので、私が探しているものと比較する必要があります。比較は常に失敗し、私は理由を理解できません。構成ファイルの解析にCの文字列一致エラーが発生しました
#include <stdio.h>
#include <stdlib.h>
void Parser(char *CONFIG_FILE, int *P_VALUE, char *STRING_TO_LOOK_FOR);
int main(){
int VALUE;
Parser("config.txt", &VALUE, "Test");
printf("%d \n", VALUE);
}
Parser.c
のmain.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void Parser(char *CONFIG_FILE, int *P_VALUE, char *STRING_TO_LOOK_FOR){
FILE *FP=fopen(CONFIG_FILE,"a+");
char TMP[256]={0x0};
int i = 1;
while(FP!=NULL && fgets(TMP, sizeof(TMP), FP)!=NULL){ //Loop through every line
i=i+1; //Increment the line number
if (strstr(TMP, STRING_TO_LOOK_FOR)){ //Is the term im looking for in this line
char *NAME_OF_CONFIG_STR = strtok(TMP, "= "); //look for delimiter
char *STRVALUE = strtok(NULL, "= "); //Get everything past the delimiter
char *P_PTR;
char *pos;
if ((pos=strchr(NAME_OF_CONFIG_STR, '\n')) != NULL){ //attempt remove \n doesn't work
*pos = '\0';
}
if(strcmp(STRING_TO_LOOK_FOR, NAME_OF_CONFIG_STR) == 0){ //try to check the two are the same
*P_VALUE = strtol(STRVALUE, &P_PTR, 10); //Returns an integer to main of the value
}
}
}
if(FP != NULL){
fclose(FP);
}
}
のconfig.txt:BLUEPIXYへ
Test= 1234
Test2= 5678
Test3= 9012
質問を適切かつ正確に説明してください。私たちに知らせてください。正確に何をしたいのですか? –
小さなプログラム、小さな入力データ、...あなたのプログラムをデバッグする方法を学び始めるのに最適な場所。 – Gerhardh
_すべてを比較するfailes_: 'Test'との比較が成功し、 '1234'が取得されます。 [DEMO](https://wandbox.org/permlink/dxtr7QzBkcljNlbc) – BLUEPIXY