在建立VC项目的时候,自动生成了视图对象。 现在用视图对象的“FILE”,“OPEN” 能够打开文件,且文件名在标题上显示。 现在我要对文件进行操作,比如返回文件的路径,I/O操作。 那么就要获得文件对象。 请问如何获得文件对象以及如何在视图对象中进行文件操作? 3X。

解决方案 »

  1.   

    我通过一下代码实现了:void CMainFrame::OnFileOpen()
    {
    // TODO: Add your command handler code here
    //Show the dialog of file open
    CFileDialog dlg(TRUE, "SQL", "*.txt",OFN_HIDEREADONLY |OFN_OVERWRITEPROMPT,"Text Files(*.txt)|*.txt|Microsoft Office Excel Files(*.xls)|*.xls|All Files(*.*)|*.*||");
    if ( dlg.DoModal()!=IDOK ) 
    return; 
    //obtain the absolute path of file
    CString sFileName=dlg.GetPathName(); 
    //open file
    CStdioFile out; 
    out.Open(sFileName, CFile::modeRead); 
    CString sSql="\"", s; 
    //read file
    do
    { out.ReadString(s); 
    sSql=sSql+s+(char)10; 

    while (out.GetPosition()!=out.GetLength()); 
    out.Close();  Croot_growth3DGUIView* pView = (Croot_growth3DGUIView *)((CFrameWnd*)AfxGetMainWnd())->GetActiveView();
    pView->SendMessage(WM_PRINT, 0, (LPARAM)&sSql);
    }