Search code examples
c++mfccstring

Extract a floating point number from a CString


I want to extract a floating point number from a CString formatted as: (example extract 22.760348)

Incidence_angle(inc)[deg]                 :22.760348

Basically I am reading a plain text file containing some parameters, and I want to perform some calculations on the values. I read the file using a CStdioFile object and extracting each line using the readString method as follows:

CStdioFile result(global::resultFile,CFile::modeRead);
while( result.ReadString(tmp) )
            {
                if(tmp.Find(L"Incidence_angle(inc)[deg]") != -1)
                {
                    //extract value of theeta i here
                    // this is probably wrong
                    theeta_i = _tscanf(L"Incidence_angle(inc)[deg]  :%f",&theeta_i);
                }
            }

I tried using scanf because I couldnt think of any other way.

I apologize if this question seems very basic and stupid, but I have been stuck on it for a long time and would apppriciate some help.

edit: took out the proof of concept program I wrote, caused confusion


Solution

  • Assuming that tmp is CString, the correct code is

    CStdioFile result(global::resultFile,CFile::modeRead);
    while( result.ReadString(tmp) )
    {
    if (swscanf_s(tmp, L"Incidence_angle(inc)[deg]  :%f", &theeta_i) == 1)
        {
            // Use the float falue
        }
    }