Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

newbie question concerning file I/O in MFC

Status
Not open for further replies.

CheeseCrusader

Programmer
Joined
Jul 27, 2003
Messages
3
Location
US

hi, I'm trying to save a file to disk using CStdioFile class, and I can't seem to figure out how to make it work.

the data I want to save is actually in a CInternetFile* returned by the OpenUrl method of CInternetSession, but when I tried any of the methods such as Read() or GetLength() (hoping to Write the results into a regular CFile) nothing worked. I understand the object also has a member of type FILE* called m_pStream, so maybe that could be another means of getting at the data? please help!
 
>> when I tried any of the methods such as Read()... nothing worked.

That is a sad story... now perhaps if you provide some technical information someone might be able to help with your problem.

excerpt from faq219-2884
Code:
14.     If you are asking a question related to code, please post the shortest example of code that will reproduce the problem you're experiencing. Include relevant declarations and controls that are affected.

Provide sample result (or desired result) of your code if you can.

Please report the exact error message if you are asking a question about an error you are receiving, please...Telling us you are getting an error is unlikely to result in a useful response. "My app errored!!!" or "Please Help." will not get as quick or accurate reply as "When I run my app under these conditions I get a syntax error on this line ... The error mesage is ..."

-pete
 

sorry if I wasn't being specific enough. oddly enough there was no error *message*. the program compiles, but terminates w/o any explanation when the below function is called. commenting out the Read statement fixes that problem, but I need to read the data! also the fileLen variable equals 0 after the GetLength function is called. the same functions work fine for me elsewhere with CFile objects. so I'm hoping there's an expert of the CInternetFile class out there who can clue me in on what I'm doing wrong. thank you for your time


/* The Code: */

void CDlgDownload::OpenUrl(CString url)
{
CInternetSession session;
CInternetFile* file = NULL;
file = (CInternetFile*) session.OpenURL(url);

if (file) {

long fileLen;
file->GetLength(&fileLen);
CString fileData = "";
file->Read(&fileData,fileLen);
m_data = fileData;

file->Close();
delete file;

} else {
m_out = "Page not found!";
}
}
/* end of code */
 
See…the link to the FAQ I gave you provides tips for how to post well formulated questions with the correct information with the ultimate goal of you receiving the help you desire. In other words it’s to your benefit to post well written, complete, concise questions.

Your stated
>> the program compiles, but terminates w/o any explanation

The thing is… the code you posted does not compile

>> file->GetLength(&fileLen);

The prototype of CInternetFile::GetLength as found here:
Is
Code:
virtual ULONGLONG GetLength( ) const;

Also you can’t send the address of a CString object to the CInternetFile::Read() method who’s prototype is:
Code:
 virtual UINT Read(
   void* lpBuf,
   UINT nCount 
);

You can find an example of exactly what you are attempting to do. They hide them in the documentation.




-pete
 
alright, palbano, those are good points. guess I get too excited about using CString everywhere.

I actually found a method recently that works with those CInternetFile's, in this thread in a different forum:


the third post shows a while loop that does the reading bit (ampersand, "gt" and ; are supposed to be a ">" ) and the fourth post explains that the Read method won't work unless you specify that the file is opened in binary mode. that must be why I didn't see a problem using that method with CFile before, because I always used binary mode!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top