vc 添加開啟檔案對話方塊並讀取檔案

來源:互聯網
上載者:User

 1.建立開啟檔案對話方塊:  
                                CFileDialog dlg(TRUE,//TRUE是建立開啟檔案對話方塊,FALSE則建立的是儲存檔案對話方塊
                                 ".txt",//預設的開啟檔案的類型
                                 NULL,//預設開啟的檔案名稱
                                 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,//開啟唯讀檔案
                                "文字檔(*.txt)|*.txt|所有檔案 (*.*)|*.*||");//所有可以開啟的檔案類型
2.點開啟檔案對話方塊上面的確定鍵後
                                                if(dlg.DoModal()==IDOK)  
                                                 {
                                                  CString m_FilePath = dlg.GetPathName();////////取出檔案路徑
                                                  CString  m_path;
                                                 m_path=m_FilePath;//將檔案的路徑放入m_path
                                                 UpdateData(FALSE);
                                                 }
3.開啟檔案:File.Open(m_path,CFile::modeRead);
4.逐行讀取檔案:CStdioFile File;///可以逐行讀檔案的類
                         CString   strLine;    
                         while(File.ReadString(strLine))   //////將每行都放進strLine字串裡
                         {    
                          AfxMessgeBox(strLine);
                         }  
5判斷讀出來的字:
                          strLine=“1|2|3|”;//要判斷的字串
                          int strIndex1  = strLine.Find('|');//在字串中尋找“|”
                          CString a[11];
                          if(-1 != strIndex1)//只要找到“|”就不會返回-1
                          {
                           int i=0;
                               while(  -1 != strIndex1)//
                               {    
                                   strIndex1 = strLine.Find('|');
                                   a[i] = strLine.Left(strIndex1);
                                  strLine = strLine.Right(strLine.GetLength() - strIndex1-1) ;
                                   i++;
                                  if (i > 10)//退出迴圈
                                  break;
                                 }
                       }

本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/halibobo520/archive/2008/11/25/3371944.aspx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.