c - convert values in string to float array -


I have a string in C that I got from some algorithms. Its format has numeric values ​​

< Pre- 0.100 0.840 0.030 0.460 0.760 -0.090

and so on

requires numerical processing to store these numerical values ​​in the float array. New to C and string handling in C should be complicated Can someone tell me how I can apply it Yes.

Do all the values ​​in a string, such as "0.100 0.840 0.030 ..." or you have different A bunch of strings, like "0.100", "0.840", "0.030", etc.? If they are in a string, are they different from whitespace (tabs, spaces, newlines) or printing characters (commas, semicolons)? Do you know how much value you have ahead of time (i.e., how big will your float array be)?

123.45 "; four * unchanged; double value; value = strotode (value text, and unbalanced); if (! Uninstaller (* unchanged) & amp; amp; amp; * unbelievable! = 0 ) {/ ** * Input string contains a character which is not valid * In a floating point constant * /}

for the description strtod () Read on Continuous will indicate the first character in that string not converted by strtod () ; If this is not the location of the brain or 0, then your string is not formatted properly for the floating point value and it should be rejected.

If all the values ​​are in a string , You have to break the string in different tokens. This is the easiest (if unsafe) way to do this strtok () :

char input [] = " 1.2 2.3 3.4 4.5 5.6 6.7 7.8 "to use; four * delim =" "; separate input / input from spaces Char * token = null; (Token = strotok (input, daily); token! = Tap; token = strotok (null, daily)) {four * unchanged; Double Values ​​= Strokes (tokens, and unbalanced); If (! Displacement (* unchanged) and amp; amp; * unconverted! = 0) {/ ** * There is a character in the input string that is not valid * in a floating point constant * /}}

Read on strtok () for details

If you do not know how much value you have, then you have to manage some of the memory. You can allocate an array of floats of some initial shapes by dynamically using malloc () or realloc () and then periodically call it Realloc () :

  define #INITIAL_EXTENT10 double * array = zero; Size_t array = 0; Size_t arrayIdx = 0; Four input [] = ...; // some long input string four * delim = ...; // whatever delimiter set is, it is a four * token; / ** * Create float array in some initial shape * / array = malloc (sizeof * array * INITIAL_EXTENT)); If (array) {arraySize = INITIAL_EXTENT; } / ** * For loop * / through its input string (token = strotok (input, deliverim); token! = Tap; token = strotok (faucet, delim)) {double valle; Four * unchanged; If (arrayIdx == arraySize) {/ ** * We have reached the end of the array, so we have to expand it. * A popular approach is to double the array size instead of * to a certain extent; In this way we reduce the number of calls * to realloc (), which is relatively expensive. * * Use a temporary variable to get results; In this way, * if the realloc operation fails, we do not lose our * original indicator * / double * tmp = realloc (array, size * array * (arraySize * 2)); If (tmp! = NULL) {array = tmp; ArraySize * = 2; } And {/ ** * memory allocation failed; For this example, we just exit the loop * / fprintf (stderr, "memory allocation failed, exit loop \ n"); break; }} / ** * Convert the next token to a float value * / val = strtod (token, and unchanged); If (! Displacement (* unchanged) and amp; * unbelievable! = 0) {/ ** * bad input string Again, we just give bail * / Fprintf (stderr, "\"% s \ "is not a valid floating point number \ n", token); break; } And {array [arrayIdx ++] = val; }}  

free () Do not forget the ARAD when you complete it


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

jquery - SimpleModal Confirm fails to submit form -

php - Multiple Select with Explode: only returns the word "Array" -