Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to get String from ".txt" file and transfer to float

How to get String from ".txt" file and transfer to float

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    camuoi288
    wrote on last edited by
    #1

    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);

    _ 1 Reply Last reply
    0
    • C camuoi288

      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);

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      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.

      _Microsoft MVP (Visual C++)

      Polymorphism in C

      C 1 Reply Last reply
      0
      • _ _Superman_

        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.

        _Microsoft MVP (Visual C++)

        Polymorphism in C

        C Offline
        C Offline
        camuoi288
        wrote on last edited by
        #3

        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);

        L D 2 Replies Last reply
        0
        • C camuoi288

          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);

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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. If cx[i] already contains a single character then why are you using sscanf()? 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);

          d@nish[^] has the answer.

          1 Reply Last reply
          0
          • C camuoi288

            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);

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            camuoi288 wrote:

            sscanf(cx[i],"&c",&kytux);
            sscanf(cx[i],"&c",&kytuy);

            Seems strange to attempt to put cx[i] into both kytux and kytuy. 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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups