How to check if CFile object is valid
Hi all,
I am retrieving files from hard-disk, but also from the internet. Now I have a problem. If a file cannot be found, the object is created, but has this value
FILE * = 0x00000000 name=?
in the debugger (this info is in the tooltip when holding the mouse over the object).
Any idea how I can check if a CFile object has these values, because I can't use the file when it has these values.
Thanks!
# 1 Re: How to check if CFile object is valid
if(File)
{
DoSomething();
}
Marxxi at 2007-11-11 0:28:32 >

# 2 Re: How to check if CFile object is valid
wel,, that's what I thought too. But the object is valid :sick:
So I have a valid CFile object, but when I quickwatch or get information using the tooltip of the object, it says:
CFile * [someaddress] { FILE * 0x000000 name=? }
# 3 Re: How to check if CFile object is valid
TRY
{
CFile file(pszFileName, CFile::modeRead);
// ...
}
CATCH_ALL(e)
{
e->ReportError(); // you can see here what's going wrong
}
END_CATCH_ALL
# 4 Re: How to check if CFile object is valid
wel,, that's what I thought too. But the object is valid :sick:
So I have a valid CFile object, but when I quickwatch or get information using the tooltip of the object, it says:
CFile * [someaddress] { FILE * 0x000000 name=? }
Yep. It's a valid yard with an invalid cow inside. :D
# 5 Re: How to check if CFile object is valid
Thanks for your checking method.
I am using this code now:
bool CMyClass::FileValid(CFile * pFile)
{
// Declare variables
char szBuffer[512];
// Try to read from file
try
{
pFile->Read(szBuffer, 512);
}
catch
{
pEx->ReportError();
pEx->Delete();
return false;
}
return true;
}
Now I am facing the next problem. What if a file cannot be found on the internet. It will return a html document with "page cannot be found message".
How can I check for such an error? Because some sites have a custom error page so I cannot check for the first 100 characters or something like that.
Any ideas??
Thanks!
# 6 Re: How to check if CFile object is valid
catch what? for sure this line is wrong
do use CHttpFile and handle CInternetException
see again my first post and use TRY/CATCH MFC macros.
# 7 Re: How to check if CFile object is valid
sorry, my real code is cath (CException * pEx).
I'll try to be more specific.
I have one function, GetFile.
This function parses the filename and checks whether it should get file from internet, ftp or just network (hard-disk).
When using internet, I use netSession.OpenUrl(sURL) which returns a CStdioFile, but I immediatly put it into a CFile object.
# 8 Re: How to check if CFile object is valid
I am using this code to get a file. Can you please tell me how to check if the file is valid?
bool CInternet::GetFileUsingHttp(CString sServer, CString sObject,
INTERNET_PORT nPort, CString sURL)
{
// Declare variables
CHttpConnection * pHttpConnection;
CHttpFile * pTempFile;
// Get http connection
pHttpConnection = m_pNetSession->GetHttpConnection(sServer, NULL, nPort);
// Return the file
try
{
// Open request
pTempFile = pHttpConnection->OpenRequest("GET", sObject, NULL, 1, NULL, NULL,
INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_RELOAD);
}
catch (CInternetException * pIE)
{
// Delete exception
pIE->Delete();
return false;
}
// Put file into member variable
m_pInternetFile = pTempFile;
/*pTempFile = m_pNetSession->OpenURL(sURL, 1,
INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_RELOAD);*/
// Return result
return true;
}
It is also failing when I use TRY and CATCH_ALL.
There is one more problem with this method. What if a user needs a file which is located at:
http://www.provider.com/~username/myfile.exe
AfxParsURL will see ~username/myfile.exe as object, but ~username is part of the server, isn't it?
# 9 Re: How to check if CFile object is valid
Well, I hope THIS LINK ( http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/HTML/_core_wininet_.28.http.2c_.ftp.2c_.gopher.29.asp) can help you.