CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2011
    Posts
    11

    Trouble reading file using CFile

    Ive been trying to use the Open and Save FileDlg in MFC and then use that GetFileName to allow me to read a log file and then update a list control i have in a dialog form. Now when i run the small dialog app and click the open dialog button. It allows me to browse to the file and select it and when i press ok it comes up with "an unamed file was not found" Ive been looking at lots of CFile::Read examples but i just cant seem to see what im doing wrong here. Any ideas?

    Code:
    void CReaderDlg::OnBnClickedButton1()
    {
    char strFilter[] = { "Log Files (*.log)|*.log|All Files (*.*)|*.*||" };
    	CFileDialog FileDlg(TRUE,".log",NULL,0,strFilter);
    	if(FileDlg.DoModal()==IDOK)
    	{
    
    
    		CFile file;
    		DWORD filesize;
    		char buffer[2000];
    		filesize = file.GetLength();
    		file.Open(FileDlg.GetFileName(),CFile::modeReadWrite);
    		file.Read(buffer,filesize);
    		m_ListChat.InsertItem(0,buffer);
    		file.Close();
    		
    		
    	}
    	else
    	{
    		return;
    	}
    }

  2. #2
    Join Date
    May 2002
    Posts
    1,435

    Re: Trouble reading file using CFile

    You might want to try FileDlg.GetPathName() instead of FileDlg.GetFileName().

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Trouble reading file using CFile

    How is GetLength() supposed to work when you haven't specified the file name to CFile yet?

    What happens if the size of the file is greater than 2000?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured