c - How Read File And Separate Values -
i'm trying read file this:
ninp=20 nout=1 nlaye=3 hid=30 er=0.001 epo=100 eporep=100 pscpa=0 tip=aggr net=emi numprec=0.25 prec=nh3;nox;pm10;so2;voc rag=4
and must read values after =
, , prec
's values, must separate every single value (delimited ;
) new line , write new file like:
nh3 nox pm10 so2 voc
to read after equals symbolt there no problems, can't separate price
.
this function:
void settaggirete(char values[20][50]){ char line[50]; int = 0, j = 0; char str[10][20]; file *conf = fopen("conf.txt", "r"); if(conf == null){ printf("impossibile apripre il file conf\n"); exit(exit_failure); } //ciclo sulle linee del file di configurazione while(fgets(line, sizeof(line), configrete) != null){ // leggo valori contenuti dopo = if(i==10){ char * str = values[10]; sscanf(line, "%*[^=]=%s", values[i]); while ((token = strsep(line, ";")) != null){ str[j] = token; j++; } }else{ sscanf(line, "%*[^=]=%s", values[i]); } i++; } fclose(configrete); }
so how can separate values??
you can't assign array this
str[j] = token;
try
strcpy(str[j], token);
althought dangerous do, could
size_t length = strlen(token); if (length >= sizeof(str[j])) length = sizeof(str[j]) - 1; memcpy(str[j], token, length); str[j][length] = '\0';
notice writing safe code @ expense of trimming token, better approach use dynamic allocation.
you also, redeclared str
inside loop, delete line
char * str = values[10];
which presumably wrong depending on how declared values
.
Comments
Post a Comment