VC-MFC 在磁盘中读取文件

  1 // ReadDlg.cpp : 实现文件
  2 //
  3 
  4 #include "stdafx.h"
  5 #include "Read.h"
  6 #include "ReadDlg.h"
  7 #include "afxdialogex.h"
  8 //#include "stream.h"
  9 #include <fstream>
 10 
 11 #include <string>
 12 
 13 
 14 using namespace std;
 15 
 16 
 17 
 18 #ifdef _DEBUG
 19 #define new DEBUG_NEW
 20 #endif
 21 
 22 
 23 // 用于应用程序“关于”菜单项的 CAboutDlg 对话框
 24 
 25 class CAboutDlg : public CDialogEx
 26 {
 27 public:
 28     CAboutDlg();
 29 
 30 // 对话框数据
 31     enum { IDD = IDD_ABOUTBOX };
 32 
 33     protected:
 34     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持
 35 
 36 // 实现
 37 protected:
 38     DECLARE_MESSAGE_MAP()
 39 public:
 40 
 41 };
 42 
 43 CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
 44 {
 45 }
 46 
 47 void CAboutDlg::DoDataExchange(CDataExchange* pDX)
 48 {
 49     CDialogEx::DoDataExchange(pDX);
 50 }
 51 
 52 BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
 53 END_MESSAGE_MAP()
 54 
 55 
 56 // CReadDlg 对话框
 57 
 58 
 59 
 60 
 61 CReadDlg::CReadDlg(CWnd* pParent /*=NULL*/)
 62     : CDialogEx(CReadDlg::IDD, pParent)
 63     
 64 {
 65     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
 66 }
 67 
 68 void CReadDlg::DoDataExchange(CDataExchange* pDX)
 69 {
 70     CDialogEx::DoDataExchange(pDX);
 71     DDX_Control(pDX, IDC_LIST_READ, m_List_Read);
 72     DDX_Control(pDX, IDC_BUTTON_READ, m_Combo_Read);
 73 
 74     //DDX_Control(pDX, IDC_COMBO_MAREAD, m_Combo_Maread);
 75     DDX_Control(pDX, IDC_LIST_NAME, m_List_Name);
 76     DDX_Control(pDX, IDC_EDIT_USER, m_Edit_User);
 77     DDX_Control(pDX, IDC_EDIT_NAME2, m_Edit_Name2);
 78     DDX_Control(pDX, IDC_EDIT_Jiequ, m_Edit_Jiequ);
 79     DDX_Control(pDX, IDC_EDIT_Jiequ1, m_Edit_Jiequ1);
 80 }
 81 
 82 BEGIN_MESSAGE_MAP(CReadDlg, CDialogEx)
 83     ON_WM_SYSCOMMAND()
 84     ON_WM_PAINT()
 85     ON_WM_QUERYDRAGICON()
 86     ON_BN_CLICKED(IDC_BUTTON_READ, &CReadDlg::OnBnClickedButtonRead)
 87     
 88 END_MESSAGE_MAP()
 89 
 90 
 91 // CReadDlg 消息处理程序
 92 
 93 BOOL CReadDlg::OnInitDialog()
 94 {
 95     CDialogEx::OnInitDialog();
 96 
 97     // 将“关于...”菜单项添加到系统菜单中。
 98 
 99     // IDM_ABOUTBOX 必须在系统命令范围内。
100     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
101     ASSERT(IDM_ABOUTBOX < 0xF000);
102 
103     CMenu* pSysMenu = GetSystemMenu(FALSE);
104     if (pSysMenu != NULL)
105     {
106         BOOL bNameValid;
107         CString strAboutMenu;
108         bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
109         ASSERT(bNameValid);
110         if (!strAboutMenu.IsEmpty())
111         {
112             pSysMenu->AppendMenu(MF_SEPARATOR);
113             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
114         }
115     }
116 
117     // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
118     //  执行此操作
119     SetIcon(m_hIcon, TRUE);            // 设置大图标
120     SetIcon(m_hIcon, FALSE);        // 设置小图标
121 
122     ShowWindow(SW_MAXIMIZE);
123 
124     // TODO: 在此添加额外的初始化代码
125     clear_all();
126 
127     return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
128 }
129 
130 void CReadDlg::OnSysCommand(UINT nID, LPARAM lParam)
131 {
132     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
133     {
134         CAboutDlg dlgAbout;
135         dlgAbout.DoModal();
136     }
137     else
138     {
139         CDialogEx::OnSysCommand(nID, lParam);
140     }
141 }
142 
143 // 如果向对话框添加最小化按钮,则需要下面的代码
144 //  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
145 //  这将由框架自动完成。
146 
147 void CReadDlg::OnPaint()
148 {
149     if (IsIconic())
150     {
151         CPaintDC dc(this); // 用于绘制的设备上下文
152 
153         SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
154 
155         // 使图标在工作区矩形中居中
156         int cxIcon = GetSystemMetrics(SM_CXICON);
157         int cyIcon = GetSystemMetrics(SM_CYICON);
158         CRect rect;
159         GetClientRect(&rect);
160         int x = (rect.Width() - cxIcon + 1) / 2;
161         int y = (rect.Height() - cyIcon + 1) / 2;
162 
163         // 绘制图标
164         dc.DrawIcon(x, y, m_hIcon);
165     }
166     else
167     {
168         CDialogEx::OnPaint();
169     }
170 }
171 
172 //当用户拖动最小化窗口时系统调用此函数取得光标
173 //显示。
174 HCURSOR CReadDlg::OnQueryDragIcon()
175 {
176     return static_cast<HCURSOR>(m_hIcon);
177 
178 
179 }
180 
181 //屏蔽回车
182 BOOL CReadDlg::PreTranslateMessage(MSG* pMsg)
183 {
184     // TODO: 在此添加专用代码和/或调用基类
185 
186     // TODO: Add your specialized code here and/or call the base class
187     
188     if(pMsg->message == WM_KEYDOWN)
189     {
190         switch(pMsg->wParam)
191         {
192             case VK_RETURN:    // 屏蔽回车
193                 {
194                     
195 
196                     // //显示时间
197                     //CTime t = CTime::GetCurrentTime(); 
198                     //int y=t.GetYear(); 
199                     //int m=t.GetMonth();
200                     //int d=t.GetDay();
201                     //int hh=t.GetHour();
202                     //int mm=t.GetMinute();
203                     //int ss=t.GetSecond();
204 
205                     //year_str.Format("%d",y);
206 
207                     //if(m<10)
208                     //{
209                     //    month_str.Format("0%d",m);
210                     //}
211                     //else
212                     //{
213                     //    month_str.Format("%d",m);
214                     //}
215                     //if(d<10)
216                     //{
217                     //    day_str.Format("0%d",d);
218                     //}
219                     //else
220                     //{
221                     //    day_str.Format("%d",d);
222                     //}
223                     //if(hh<10)
224                     //{
225                     //    hour_str.Format("0%d",hh);
226                     //}
227                     //else
228                     //{
229                     //    hour_str.Format("%d",hh);
230                     //}
231                     //if(mm<10)
232                     //{
233                     //    minute_str.Format("0%d",mm);
234                     //}
235                     //else
236                     //{
237                     //    minute_str.Format("%d",mm);
238                     //}
239                     //if(ss<10)
240                     //{
241                     //    second_str.Format("0%d",ss);
242                     //}
243                     //else
244                     //{
245                     //    second_str.Format("%d",ss);
246                     //}
247 
248                     //m_Combo_Year.SetWindowTextA(year_str);
249                     //m_Combo_Month.SetWindowTextA(month_str);
250                     //m_Combo_Day.SetWindowTextA(day_str);
251                     //m_Combo_Hour.SetWindowTextA(hour_str);
252                     //m_Combo_Minute.SetWindowTextA(minute_str);
253                     //m_Combo_Second.SetWindowTextA(second_str);
254 
255                 
256                 
257                          
258      
259                 }
260             return true;
261         }
262     }
263 
264     return CDialogEx::PreTranslateMessage(pMsg);
265 
266     
267 
268 }
269 //初始化数据
270 void CReadDlg::clear_all()
271 {
272 
273     //((CComboBox*)GetDlgItem(IDC_COMBO_MAREAD))->GetWindowText(mac_no_str);
274     //表格数据(1)
275     m_List_Name.ModifyStyle( 0L, LVS_SHOWSELALWAYS );            
276     m_List_Name.SetExtendedStyle( LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_CHECKBOXES );//item前生成checkbox控件
277 
278     CRect   linshi_rect;
279     int   linshi_nWidth;
280     
281     m_List_Name.GetClientRect(linshi_rect);
282     linshi_nWidth = linshi_rect.Width();
283     m_List_Name.InsertColumn(1,"文件名称",LVCFMT_CENTER,linshi_nWidth*2/2,0);
284 
285 
286     //表格数据(2)
287     m_List_Read.ModifyStyle( 0L, LVS_SHOWSELALWAYS );            
288     m_List_Read.SetExtendedStyle( LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_CHECKBOXES );//item前生成checkbox控件
289 
290     
291     
292     /*CRect   linshi_rect;
293     int   linshi_nWidth;*/
294     
295     //m_List_Read.GetClientRect(linshi_rect);
296     //linshi_nWidth = linshi_rect.Width();
297     //m_List_Read.InsertColumn(1,"Id",LVCFMT_CENTER,linshi_nWidth*2/16,0);
298     //m_List_Read.InsertColumn(2,"名称",LVCFMT_CENTER,linshi_nWidth*2/16,0);
299     //m_List_Read.InsertColumn(3,"测量值",LVCFMT_CENTER,linshi_nWidth*2/16,0);
300     //m_List_Read.InsertColumn(4,"标准值",LVCFMT_CENTER,linshi_nWidth*2/16,0);
301     //m_List_Read.InsertColumn(5,"上公差",LVCFMT_CENTER,linshi_nWidth*2/16,0);
302     //m_List_Read.InsertColumn(6,"下公差",LVCFMT_CENTER,linshi_nWidth*2/16,0);
303     //m_List_Read.InsertColumn(7,"误差",LVCFMT_CENTER,linshi_nWidth*2/16,0);
304     //m_List_Read.InsertColumn(8,"结果",LVCFMT_CENTER,linshi_nWidth*2/16,0);
305 
306 
307 
308     
309     m_List_Read.InsertColumn(1,"名称",LVCFMT_CENTER,linshi_nWidth*2/14,0);
310     m_List_Read.InsertColumn(2,"测量值",LVCFMT_CENTER,linshi_nWidth*2/14,0);
311     m_List_Read.InsertColumn(3,"标准值",LVCFMT_CENTER,linshi_nWidth*2/14,0);
312     m_List_Read.InsertColumn(4,"上公差",LVCFMT_CENTER,linshi_nWidth*2/14,0);
313     m_List_Read.InsertColumn(5,"下公差",LVCFMT_CENTER,linshi_nWidth*2/14,0);
314     m_List_Read.InsertColumn(6,"误差",LVCFMT_CENTER,linshi_nWidth*2/14,0);
315     m_List_Read.InsertColumn(7,"结果",LVCFMT_CENTER,linshi_nWidth*2/14,0);
316 
317 
318 
319 }
320 
321 //读取数据按钮
322 void CReadDlg::OnBnClickedButtonRead()
323 {
324     // TODO: 在此添加控件通知处理程序代码
325 
326 
327     load_readname();
328     
329     load_read();
330 
331     
332 
333 }
334 
335 //读取文件名
336 bool CReadDlg::load_readname(void)
337 {
338     //获得目录中指定文件名的方法:(第一种)
339     
340     m_List_Name.DeleteAllItems();                //清空标签打印格式列表数据
341 
342     CFileFind   finder; 
343     CString   strWildcard; 
344     CString   str;
345     strWildcard   =   "D:\\Studay_work\\test*.txt "; 
346     BOOL bWorking = finder.FindFile(strWildcard);  
347     int j=0;
348     while(bWorking!=0)
349     {
350         bWorking = finder.FindNextFile();  
351         if(bWorking!=0)
352         {         
353             str= finder.GetFileName(); 
354             m_List_Name.InsertItem(j,str);
355             
356 
357         
358             j++;
359         }
360         else
361         {
362             str= finder.GetFileName();
363             m_List_Name.InsertItem(j,str);
364             m_Edit_Name2.SetWindowTextA(str);
365             
366 
367             j++;
368         }
369     }
370     finder.Close();
371 
372 
373     return true;
374 }
375 
376  
377 //第二种 获取文件数据的方法
378 bool CReadDlg::load_read(void)
379 {
380     bool file_found_flag=false;
381     CFileFind   finder; 
382     CString   strWildcard; 
383     strWildcard   = "D:\\Studay_work\\test.txt "; 
384     BOOL bWorking = finder.FindFile(strWildcard);  
385     if(bWorking!=0)
386     {
387         bWorking = finder.FindNextFile();  
388         file_found_flag = true;
389     }
390     finder.Close(); 
391 
392     if(file_found_flag)
393     {
394         file_found_flag = false;
395 
396         CString fileName="D:\\Studay_work\\test.txt "; 
397         CStdioFile file;
398 
399         file.Open(fileName,CFile::modeRead,0);    
400 
401         CString read_str;
402 
403             
404           
405         //for(i=0;i<8;i++)
406 
407 
408         while(file.ReadString(read_str))  
409         {  
410 
411 
412             //ifstream in("D:\\Studay_work\\test.txt ");
413          //   string line;
414  
415          //   if(in) // 有该文件
416          //   {
417          //       while (getline (in, line)) // line中不包括每行的换行符
418          //       { 
419          //           cout << line << endl;
420 
421          //       }
422          //   }
423          //   else // 没有该文件
424          //   {
425          //       cout <<"no such file" << endl;
426          //   }
427             
428  
429            //m_List_Read.DeleteAllItems();
430            //CString str;
431 
432            //int Row=m_List_Read.GetSelectionMark();    
433 
434 
435 
436 
437 
438            unsigned int i=0;
439            CString mac_no_str1;    //取第一值 名称i
440            unsigned int pos_int;
441 
442            
443             pos_int=read_str.Find(";");
444             mac_no_str1=read_str.Left(pos_int);
445             m_Edit_Jiequ.SetWindowTextA(mac_no_str1);
446             m_List_Read.InsertItem(i,mac_no_str1);
447             AfxMessageBox(mac_no_str1);
448                        
449           
450  
451             //取第二值  测量值
452             read_str=read_str.Right(read_str.GetLength()-1-pos_int);
453             pos_int=read_str.Find(";");
454             mac_no_str1=read_str.Left(pos_int);
455             m_Edit_Jiequ1.SetWindowTextA(mac_no_str1);
456             m_List_Read.SetItemText(i,1,mac_no_str1);
457             AfxMessageBox(mac_no_str1);
458            
459            
460             //取第三值   标准值
461             read_str=read_str.Right(read_str.GetLength()-1-pos_int);
462             pos_int=read_str.Find(";");
463             mac_no_str1=read_str.Left(pos_int);
464             m_List_Read.SetItemText(i,2,mac_no_str1);
465             AfxMessageBox(mac_no_str1);
466 
467 
468             //第四值    上公差
469 
470             read_str=read_str.Right(read_str.GetLength()-1-pos_int);
471             pos_int=read_str.Find(";");
472             mac_no_str1=read_str.Left(pos_int);
473             m_List_Read.SetItemText(i,3,mac_no_str1);
474             AfxMessageBox(mac_no_str1);
475 
476             //第五值      下公差
477             read_str=read_str.Right(read_str.GetLength()-1-pos_int);
478             pos_int=read_str.Find(";");
479             mac_no_str1=read_str.Left(pos_int);
480             m_List_Read.SetItemText(i,4,mac_no_str1);
481             AfxMessageBox(mac_no_str1);
482 
483             //第六值    误差
484             read_str=read_str.Right(read_str.GetLength()-1-pos_int);
485             pos_int=read_str.Find(";");
486             mac_no_str1=read_str.Left(pos_int);
487             m_List_Read.SetItemText(i,5,mac_no_str1);
488             AfxMessageBox(mac_no_str1);
489 
490             //第七值    结果
491             read_str=read_str.Right(read_str.GetLength()-1-pos_int);
492             pos_int=read_str.Find(";");
493             mac_no_str1=read_str.Left(pos_int);
494             m_List_Read.SetItemText(i,6,mac_no_str1);
495             AfxMessageBox(mac_no_str1);
496 
497          
498         //取到了第一个数据
499 
500         /*CString str1,str3;
501         str3="123456;ABCD1234;7890;1234";  //str3= read_str;
502 
503         unsigned int pos_int;              
504         pos_int=str3.Find(";");
505         str1=str3.Left(pos_int);
506         AfxMessageBox(str1);
507 
508         str3=str3.Right(str3.GetLength()-1-pos_int);
509         pos_int=str3.Find(";");
510         str1=str3.Left(pos_int);
511         AfxMessageBox(str1);
512 
513         str3=str3.Right(str3.GetLength()-1-pos_int);
514         pos_int=str3.Find(";");
515         str1=str3.Left(pos_int);
516         AfxMessageBox(str1); */
517 
518 
519            
520           // AfxMessageBox(mac_no_str);   _____
521           // m_Edit_User.SetWindowTextA(mac_no_str);_____
522            
523          
524          //______________________________________________
525                 /*CString str=""; 
526                 int n = str.ReverseFind(';') ;
527                 str = str.Left(str.GetLength()-n-1);
528                 m_Edit_Jiequ.SetWindowTextA(str);
529 
530 */          
531           
532 
533 
534             ///unsigned int pos_int;            
535            /// pos_int=mac_no_str.Find(";");
536             ///mac_no_str=mac_no_str.Left(pos_int);//:的第一个
537         //    str=str.Mid(pos_int);  //;后的所有数据
538         //    mac_no_str=mac_no_str.Right(pos_int);//;最后面的数据
539             //AfxMessageBox(str);
540             //AfxMessageBox(read_str);//读取所有的数据
541             ///m_Edit_Jiequ.SetWindowTextA(mac_no_str);
542         ///    AfxMessageBox(mac_no_str);//第一个数据
543             
544             //取到第二个数据
545             ///unsigned int pos_int1 ;             
546             ///pos_int1=mac_no_str1.Find(";");
547             //mac_no_str1=mac_no_str1.Mid(pos_int1);
548             //mac_no_str1 = mac_no_str1.Mid(mac_no_str1.GetLength()-pos_int1-1);
549             ///mac_no_str1 = mac_no_str1.Right(mac_no_str1.GetLength()-1-mac_no_str1.Find(_T(";"))); //没有第二个数据
550             
551             ///mac_no_str1 = mac_no_str1.Left(mac_no_str1.GetLength()-27-mac_no_str1.Find(_T(";")));//第一个 第二个数据
552             //mac_no_str1=mac_no_str1.Right(pos_int1);
553             //AfxMessageBox(read_str);//读取所有的数据
554             ///m_Edit_Jiequ1.SetWindowTextA(mac_no_str1);
555             ///AfxMessageBox(mac_no_str1);//第一个数据
556             
557             
558 
559             //取到第三个数据
560             ///unsigned int pos_int2 ;             
561             ///pos_int2=mac_no_str2.Find(";");
562             //mac_no_str1=mac_no_str1.Mid(pos_int1);
563             //mac_no_str1 = mac_no_str1.Mid(mac_no_str1.GetLength()-pos_int1-1);
564             
565             ///mac_no_str2 = mac_no_str2.Right(mac_no_str2.GetLength()-5-mac_no_str2.Find(_T(";"))); //没有第二个数据
566 
567             ///mac_no_str2 = mac_no_str2.Left(mac_no_str2.GetLength()-22-mac_no_str2.Find(_T(";")));//第一个 第二个数据
568             
569         
570             //mac_no_str1=mac_no_str1.Right(pos_int1);
571             //AfxMessageBox(read_str);//读取所有的数据
572             ///m_Edit_Jiequ1.SetWindowTextA(mac_no_str2);
573             ///AfxMessageBox(mac_no_str2);//第一个数据
574            
575 
576         }
577 
578      
579        file.Close();
580     }
581     else
582     {
583 
584     
585         AfxMessageBox("mac_no临时数据文件不存在!");
586 
587     }
588 
589 
590 
591        return true;
592 }

 

 

 

 

posted on 2021-09-03 09:40  Bytezero!  阅读(203)  评论(0编辑  收藏  举报