Click to See Complete Forum and Search --> : CFile::Read
Gaetan Emond
June 2nd, 1999, 04:36 PM
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.
Chao
June 2nd, 1999, 08:02 PM
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
Gaetan Emond
June 3rd, 1999, 09:17 AM
I understand but how could i get the contents of the file. I would like to see that contents in a dialog box.
Todd Jeffreys
June 3rd, 1999, 11:30 AM
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();
}
Chao
June 3rd, 1999, 08:07 PM
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
Gaetan Emond
June 14th, 1999, 12:44 PM
Thank you but it didn't work.
Sam Hobbs
June 14th, 1999, 03:15 PM
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
}
Gaetan Emond
June 21st, 1999, 02:26 PM
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.
Chao
June 21st, 1999, 08:45 PM
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
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.