-
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.
-
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
-
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.
-
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();
}
-
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
-
Re: CFile::Read
Thank you but it didn't work.
-
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
}
-
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.
-
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