Float string to float Array

Source: Internet
Author: User
Tags float number

A small problem occurred a few days ago.

0 0 524 1049 0.9171 113029 323142 87392 1524 1664 0.9973 4537 0.5154 33.1722 33.3961

5938 11 116 253 2.3992 74985 479837 347454 2734 12 2836 0.9974 997 0.4939 26.0862

........................

Each row of numbers in the TXT file is extracted to a float array, and then the subsequent judgment is performed. After thinking for a long time, we finally have a solution.

First define the cstdiofile variable, and then use file. Open to open the data file data.txt to be converted, and then while (file. readstring (STR) reads the string of a row to the cstring type variable STR each time. Then call the void transf function (cstring STR, float result [16]) // each row has 16 numbers.

Converts STR to a float array. The following describes the implementation process of Transf.

 

 

 

 

Void transf (cstring STR, float result [16])
{
Int num = Str. getlength (); // obtain the string length.
Char * Buf;
Char TMP [10] = {0}; // maximum number of digits
Int m;
Int * statu;
Bool f_transf = false; // whether to convert a string to a float number
Int count_str = 0; // Add one character to each STR
Int count_num = 0; // Add one to each float number to be converted
Buf = (char *) malloc (sizeof (char) * num); // allocate memory space for strings in Str
Statu = (bool *) malloc (sizeof (bool) * num); // allocate memory space for the status of each character in Str
Memset (statu,-1, num); // initialization status
Memcpy (BUF, str. getbuffer (0), num); // copy the characters in STR to Buf.
For (INT I = 0; I <num; I ++) // read one character each time
{M = Buf [I]; // temporary characters in m
If (M> = '0' & M <= '9') | M = '.') // if the character is a number or a decimal point
{TMP [count_str] = m; // store it in TMP and convert it into a number when the conditions are met
Count_str ++;
Statu [I] = 1; // The current character status is a number or decimal point

}
Else if (M = 32 | M = 9) // whether the current character is a space or a tab key (each number in each line of the string is separated by a space or a tab key)
{Statu [I] = 0; // The current character status is a separator

}

If (count_num = 15 & (statu [I-1] = 0 & statu [I] = 1) // process the last number in the string
{
Memcpy (TMP, & Buf [I], num-I );
Result [count_num] = (float) atof (TMP );

}
If (statu [I-1] = 1 & statu [I] = 0) // if the first character is a number or decimal point, the last character is a separator, convert TMP to a float number.
{Result [count_num] = (float) atof (TMP );
Count_num ++;
Count_str = 0;
Memset (TMP, 0, 10 );
}

}

Free (BUF); // release memory space
Free (statu );
}

 

 

There are certainly many ways to extract numbers from strings into arrays. Maybe a function can be implemented. Please provide more instructions.

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.