How to get String from ".txt" file and transfer to float
-
I want to get String as : "6.591E+02" from text file,and transfer it to float.My file have a form: STT x y 1 6.591E+02 8.575E+02 2 7.350E+02 9.289E+02 3 7.234E+02 1.078E+03 4 6.844E+02 1.191E+03 5 5.951E+02 1.306E+03 6 4.125E+02 1.259E+03 my code as:
CString m_file; UINT nOpenFlags; nOpenFlags = CFile ::modeRead|CFile ::modeCreate; UpdateData(TRUE); CFileDialog dlg(TRUE,0,0,OFN_HIDEREADONLY | OFN_FILEMUSTEXIST,"All Files (*.txt)|*.txt||"); if (dlg.DoModal()==IDCANCEL) return; m_file=dlg.GetPathName(); int i,count,j; count =0; UINT nOpenFlags1; CStdioFile Inputfile; CFileException File; CString s; tamthoi=""; nOpenFlags1=CFile ::modeRead; if(!Inputfile.Open(m_file,nOpenFlags1,&File)) { File.ReportError(); } while(Inputfile.ReadString(s)) { count++; tamthoi+=s; } Inputfile.Close(); count_line=count; if(!Inputfile.Open(m_file,nOpenFlags1,&File)) { File.ReportError(); } m_Listfile.ResetContent(); for(i=0;i<count_line;i++) { Inputfile.ReadString(s); m_Listfile.AddString(s); } Inputfile.Close(); CStringArray cx,cy; cx.SetSize(count_line); cy.SetSize(count_line); x= new float[count_line]; y= new float [count_line]; for (i=1;i<count_line;i++) { int c; char *kytux,*kytuy; c=tamthoi.Find(" ",21*(i-1)+9); cx[i]=tamthoi.Mid(c+1,9); cy[i]=tamthoi.Mid(c+6,9); sscanf(cx[i],"&c",&kytux); sscanf(cx[i],"&c",&kytuy); x[i]=atof(kytux); y[i]=atof(kytuy); } UpdateData(FALSE);
-
I want to get String as : "6.591E+02" from text file,and transfer it to float.My file have a form: STT x y 1 6.591E+02 8.575E+02 2 7.350E+02 9.289E+02 3 7.234E+02 1.078E+03 4 6.844E+02 1.191E+03 5 5.951E+02 1.306E+03 6 4.125E+02 1.259E+03 my code as:
CString m_file; UINT nOpenFlags; nOpenFlags = CFile ::modeRead|CFile ::modeCreate; UpdateData(TRUE); CFileDialog dlg(TRUE,0,0,OFN_HIDEREADONLY | OFN_FILEMUSTEXIST,"All Files (*.txt)|*.txt||"); if (dlg.DoModal()==IDCANCEL) return; m_file=dlg.GetPathName(); int i,count,j; count =0; UINT nOpenFlags1; CStdioFile Inputfile; CFileException File; CString s; tamthoi=""; nOpenFlags1=CFile ::modeRead; if(!Inputfile.Open(m_file,nOpenFlags1,&File)) { File.ReportError(); } while(Inputfile.ReadString(s)) { count++; tamthoi+=s; } Inputfile.Close(); count_line=count; if(!Inputfile.Open(m_file,nOpenFlags1,&File)) { File.ReportError(); } m_Listfile.ResetContent(); for(i=0;i<count_line;i++) { Inputfile.ReadString(s); m_Listfile.AddString(s); } Inputfile.Close(); CStringArray cx,cy; cx.SetSize(count_line); cy.SetSize(count_line); x= new float[count_line]; y= new float [count_line]; for (i=1;i<count_line;i++) { int c; char *kytux,*kytuy; c=tamthoi.Find(" ",21*(i-1)+9); cx[i]=tamthoi.Mid(c+1,9); cy[i]=tamthoi.Mid(c+6,9); sscanf(cx[i],"&c",&kytux); sscanf(cx[i],"&c",&kytuy); x[i]=atof(kytux); y[i]=atof(kytuy); } UpdateData(FALSE);
Please mention the exact problem that you're facing. You could understand the problem better by putting a breakpoint and then single stepping through the code in the debugger.
«_Superman_» _I love work. It gives me something to do between weekends.
-
Please mention the exact problem that you're facing. You could understand the problem better by putting a breakpoint and then single stepping through the code in the debugger.
«_Superman_» _I love work. It gives me something to do between weekends.
My problem is when i want show a result at Editbox(only show a element of array) then it's errored,and Editbox don't show anything.:(.I want to know be this code errored?:
for (i=1;i<count_line;i++) { int c; char *kytux,*kytuy; c=tamthoi.Find(" ",21*(i-1)+9); cx[i]=tamthoi.Mid(c+1,9); cy[i]=tamthoi.Mid(c+6,9); sscanf(cx[i],"&c",&kytux); sscanf(cx[i],"&c",&kytuy); x[i]=atof(kytux); y[i]=atof(kytuy);
-
My problem is when i want show a result at Editbox(only show a element of array) then it's errored,and Editbox don't show anything.:(.I want to know be this code errored?:
for (i=1;i<count_line;i++) { int c; char *kytux,*kytuy; c=tamthoi.Find(" ",21*(i-1)+9); cx[i]=tamthoi.Mid(c+1,9); cy[i]=tamthoi.Mid(c+6,9); sscanf(cx[i],"&c",&kytux); sscanf(cx[i],"&c",&kytuy); x[i]=atof(kytux); y[i]=atof(kytuy);
camuoi288 wrote:
sscanf(cx[i],"&c",&kytux);
Your format constant is incorrect (should be
"%c"
), and you are trying to store the response into an uninitialised (and wrong type of) variable. Ifcx[i]
already contains a single character then why are you usingsscanf()
? Also please use <pre></pre> tags around your code (and indentation) to make it readable, like this:for (i=1; i > count_line; i++)
{
int c;
char *kytux,*kytuy;
c=tamthoi.Find(" ",21*(i-1)+9);
cx[i]=tamthoi.Mid(c+1,9);
cy[i]=tamthoi.Mid(c+6,9);
sscanf(cx[i],"&c",&kytux);
sscanf(cx[i],"&c",&kytuy);
x[i]=atof(kytux);
y[i]=atof(kytuy); -
My problem is when i want show a result at Editbox(only show a element of array) then it's errored,and Editbox don't show anything.:(.I want to know be this code errored?:
for (i=1;i<count_line;i++) { int c; char *kytux,*kytuy; c=tamthoi.Find(" ",21*(i-1)+9); cx[i]=tamthoi.Mid(c+1,9); cy[i]=tamthoi.Mid(c+6,9); sscanf(cx[i],"&c",&kytux); sscanf(cx[i],"&c",&kytuy); x[i]=atof(kytux); y[i]=atof(kytuy);
camuoi288 wrote:
sscanf(cx[i],"&c",&kytux);
sscanf(cx[i],"&c",&kytuy);Seems strange to attempt to put
cx[i]
into bothkytux
andkytuy
. Why not try:x[i] = atof(cx[i]);
y[i] = atof(cy[i]);"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather