Sunday, 15 February 2015

c - How Read File And Separate Values -


I'm trying to read a file like this:

  nInp = 20 nOut = 1 NLe = 3 hid = 30 er = 0.001 apo = 100 aprap = 100 ssppa = 0 tip = aggregation = emi number diff = 0.25 pt = h3; NOx; PM10 SO2; VOC Raga = 4  

and I have to read only the values ​​after = , and with values ​​of prec , every one of me The value must be separated (; ) with a new line and then I write them in a new file like:

  NH3 NOX PM10 SO2 VOC  

There is no problem equal to the symbols after reading, but I can not separate the value .

This is my job:

  zero retirement (RER value [20] [50]) {four lines [50]; Int i = 0, j = 0; Four letters [10] [20]; FILE * conf = fopen ("conf.txt", "r"); If (Conf == NULL) {printf ("Impossibile apripre il file conf \ n"); Exhaust (EXIT_FAILURE); } // when configuring the file during file (fgets (line, size (line), configRete) == (// logotype valori contenuti dopo = if (i == 10) {char * str = values [10]; Sscanf (line, "% * [^ =] =% s", value [i]); while ((token = strsep (line, ";")) = {null} {str [j] = Tokens; J ++;}} Else {sscanf (line, "% * [^ =] =% s", value [i]);} I ++;} Fclose (configRete);}  < / Pre> 

So how can I separate this value ??

J], Token);

It is dangerous to think, you can

  size_ty length = stroke (token); if (length> = Sizeof (str [j]) Length = sizeof (str [j]) - 1; Memcpy (str [j], token, length); Str [j] [length] = '\ 0';  

Note that you are writing a secure code at the cost of the token touch, so a better way is to use dynamic allocation.

You also, under this redeclared Str loop, delete this line

  char * str = values ​​[10];  

which you have declared that values ​​.


No comments:

Post a Comment