CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: CFile::Read

  1. #1
    Join Date
    May 1999
    Posts
    30

    CFile::Read

    Hi every one,

    That's me again. With CFile::Read, it is returning the number of byte of the file but how could i visualize the contains of the file itself. Do I have to use another function or command.

    Thank you for helping me. I really appreciate.

    Gaetan.


  2. #2
    Join Date
    Apr 1999
    Posts
    23

    Re: CFile::Read

    Hi,

    CFile::Read does not return the number of byte of the file, CFile::GetLength does. CFile::Read returns the number of bytes read from the file.

    The contents of the file is written to the buffer passed in as the first parameter of CFile::Read.

    Hope this is helpful.

    Chao


  3. #3
    Join Date
    May 1999
    Posts
    30

    Re: CFile::Read

    I understand but how could i get the contents of the file. I would like to see that contents in a dialog box.


  4. #4
    Join Date
    Apr 1999
    Posts
    396

    Re: CFile::Read

    TCHAR buffer[COUNT];
    try
    {
    UINT dBytesRead=file.Read(buffer,COUNT);
    if (dBytesRead > 0)
    {
    buffer[dBytesRead]=0;
    MessageBox(hWnd,buffer,NULL,MB_OK);
    }
    catch (CFileException *e)
    {
    e->Delete();
    }


  5. #5
    Join Date
    Apr 1999
    Posts
    23

    Re: CFile::Read

    Hi,

    You can try something like this:

    CFile myFile;
    myFile.Open(strFileName, CFile::modeRead);
    char Buf[Buffer_Size]; // buffer size > file length
    myFile.Read(Buf, sizeof(Buf));
    Buf[myFile.GetLength()]=0;//append a NULL

    Then you can treat Buf as a CString and write it to the CString member variable associated with the Editbox.

    I dunno if this is the best way to do it but it should work.

    Chao


  6. #6
    Join Date
    May 1999
    Posts
    30

    Re: CFile::Read

    Thank you but it didn't work.


  7. #7
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266

    Re: CFile::Read

    Here's another one to try. In this sample, m_FileData is a member variable created using Class Wizard for an edit box in the dialog. You should add error processing for handling open errors etc.


    BOOL CDataDlg::OnInitDialog() {
    CFileException FileExc;
    CFile Inputfile;
    DWORD n;
    CDialog::OnInitDialog();
    if (!Inputfile.Open("C:\\CONFIG.SYS", CFile::modeRead, &FileExc))
    return TRUE;
    n = __min(Inputfile.GetLength(), INT_MAX);
    n = Inputfile.Read(m_FileData.GetBuffer(n), n);
    m_FileData.ReleaseBuffer(n);
    Inputfile.Close();
    UpdateData(FALSE);
    return TRUE; // return TRUE unless you set the focus to a control
    }





    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  8. #8
    Join Date
    May 1999
    Posts
    30

    Re: CFile::Read

    Thank you very much, i wasn't doing it correctly. Now, it is working really good. The only problem that i am having right now is because i am seeing the carrage return code, i don't want to see it.


  9. #9
    Join Date
    Apr 1999
    Posts
    23

    Re: CFile::Read

    Hi,

    If I got your question correctly, you must change the properties of your edit box. Your edit box must have "multiline" and "want return" enabled. Then you should not see the CRLF pairs.

    Chao


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