我必须使用此结构和格式参数创建一个读取设置功能(我无法改变)。
文本文件:
key1:value1
key2:value2
....
格式:
typedef struct {
char *key;
char *value;
}configParam;
void loadSettings(char fileName[],configParam *paramsReaded[], int *length, int *statusCode);
和我的代码:
void loadSettings(char fileName[],configParam *paramsReaded[], int *length, int *statusCode){
*paramsReaded = (configParam*)malloc((*length)*sizeof(configParam));
int j;
for(j=0; j<*length; j++){
*paramsReaded[j].key = (char *)malloc(MAX_STRING*sizeof(char));
*paramsReaded[j].value = (char *)malloc(MAX_STRING*sizeof(char));
}
FILE *file = fopen(fileName,"rb");
char line[100];
char *token;
int i,k;
if (file == NULL){
printf("\nError de lectura: archivo no encontrado\n");
*statusCode = 0;
exit(1);
}
while (feof(file) == 0){
fgets(line,100,file);
token = strtok(line,":");
strcpy(*paramsReaded[i].key, token);
token = strtok(NULL,":");
strcpy(*paramsReaded[i].value, token);
i++;
}
fclose(file);
for(k=0; k<*length; k++){
printf ("\nclave[%d]: %s \nvalor[%d]: %s\n",k , *paramsReaded[k].key,k , *paramsReaded[k].value);
}
}
int main(){
char route[] = "settings_entrada.txt";
int lengthSettings;
int statusCode = 1; // set in 0, if finds a error
countLines(route,&lengthSettings); //only count lines of txt
configParam parametersReaded[];
loadSettings (route, ¶metersReaded,&lengthSettings,&statusCode);
}
code :: blocks给我看
请求会员&#39;
key
&#39;在不是结构或联合的东西中请求会员&#39;
value
&#39;在不是结构或联合的东西中
我不确定是否声明结构数组parametersReaded
:(