473,396 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Fetching binary file linked in URL

I have to write a console application which will fetch a zip on a daily
basis.
This zip file is hotlinked in a web page. The name of this .zip file will
change every day. I've been able to start the session, open the URL,
read each line, and get the line with the .zip file URL in it no problem.
Where I am running intro trouble is writing the zip file to disk as binary.
I'm following the example from the VC++ Online Help reference to
CFile::Open(). Can anyone see what I am doing wrong ?

thanks in advance.

#include <afx.h>
#include <afxwin.h>
#include <afxinet.h>
#include <iostream>
#include <stdlib.h>

int main(int argc, _TCHAR* argv[])
{
CStdioFile* pRemoteURLFile;
CStdioFile* pRemoteZipFile;
CFile* pLocalZipFile;
CInternetSession pSessionURL("URLSession");
CInternetSession pSessionZip("ZipSession");
char* ziptag = "shapefile";
CString sURL;
CString sZipURL;
CString sReadLine;
char* pFileName = "test.dat";
int iFound;
int iNumLines;
UINT nBytesRead;
char pbuf[100];
CFileException cFileErr;
pRemoteURLFile = NULL;
pRemoteZipFile = NULL;
pLocalZipFile = NULL;
iNumLines = nBytesRead = 0;
sURL = "http://activefiremaps.fs.fed.us/fireptdata/modisfire_2004.htm";
pRemoteURLFile =
pSessionURL.OpenURL(sURL,1,INTERNET_FLAG_TRANSFER_ BINARY,NULL,NULL);
while (pRemoteURLFile->ReadString(sReadLine))
{
iNumLines ++;
iFound = sReadLine.Find("shapefile");
if (iFound > 0)
{
sZipURL = getZIPURL(sReadLine,ziptag,sURL);
}
}
delete pRemoteURLFile;
pSessionURL.Close;

pSessionZip.OpenURL(sZipURL,1,INTERNET_FLAG_TRANSF ER_BINARY,NULL,NULL);
pRemoteZipFile =
pSessionZip.OpenURL(sZipURL,1,INTERNET_FLAG_TRANSF ER_BINARY,NULL,NULL);

//** The below line fails **
if( pLocalZipFile->Open(pFileName, CFile::modeCreate | CFile::modeWrite |
CFile::typeBinary, &cFileErr ) )
{
while (pRemoteZipFile->Read(pbuf,100))
{
//count bytes read to see if size of file is correct
nBytesRead = nBytesRead + sizeof(pbuf);

//** write binary date to pLocalZipFile here **
}
printf("%d bytes read from remote file %s\n",nBytesRead,sZipURL);
delete pRemoteZipFile;
pSessionZip.Close;
return 0;
}
}
Jul 22 '05 #1
5 4534

"David Jacques" <Da***********@ccrs.nrcan.gc.ca> wrote in message
news:cn*********@nrn2b.NRCan.gc.ca...
I have to write a console application which will fetch a zip on a daily
basis.
This zip file is hotlinked in a web page. The name of this .zip file will
change every day. I've been able to start the session, open the URL,
read each line, and get the line with the .zip file URL in it no problem.
Where I am running intro trouble is writing the zip file to disk as
binary.
I'm following the example from the VC++ Online Help reference to
CFile::Open(). Can anyone see what I am doing wrong ?

thanks in advance.


CFile appears to be an MFC class. As such, you need to ask in an mfc or
vc++ newsgroup for specifics on dealing with it. This group deals only with
standard C++ language issues, not platform or implementation-specific
issues.

-Howard
Jul 22 '05 #2
What is coming back in cFileErr from
if( pLocalZipFile->Open(pFileName, CFile::modeCreate | CFile::modeWrite |
CFile::typeBinary, &cFileErr ) ) ?

Also, are your certain your app has write permission to pFileName?

Jim H
"David Jacques" <Da***********@ccrs.nrcan.gc.ca> wrote in message
news:cn*********@nrn2b.NRCan.gc.ca...
I have to write a console application which will fetch a zip on a daily
basis.
This zip file is hotlinked in a web page. The name of this .zip file will
change every day. I've been able to start the session, open the URL,
read each line, and get the line with the .zip file URL in it no problem.
Where I am running intro trouble is writing the zip file to disk as
binary.
I'm following the example from the VC++ Online Help reference to
CFile::Open(). Can anyone see what I am doing wrong ?

thanks in advance.

#include <afx.h>
#include <afxwin.h>
#include <afxinet.h>
#include <iostream>
#include <stdlib.h>

int main(int argc, _TCHAR* argv[])
{
CStdioFile* pRemoteURLFile;
CStdioFile* pRemoteZipFile;
CFile* pLocalZipFile;
CInternetSession pSessionURL("URLSession");
CInternetSession pSessionZip("ZipSession");
char* ziptag = "shapefile";
CString sURL;
CString sZipURL;
CString sReadLine;
char* pFileName = "test.dat";
int iFound;
int iNumLines;
UINT nBytesRead;
char pbuf[100];
CFileException cFileErr;
pRemoteURLFile = NULL;
pRemoteZipFile = NULL;
pLocalZipFile = NULL;
iNumLines = nBytesRead = 0;
sURL = "http://activefiremaps.fs.fed.us/fireptdata/modisfire_2004.htm";
pRemoteURLFile =
pSessionURL.OpenURL(sURL,1,INTERNET_FLAG_TRANSFER_ BINARY,NULL,NULL);
while (pRemoteURLFile->ReadString(sReadLine))
{
iNumLines ++;
iFound = sReadLine.Find("shapefile");
if (iFound > 0)
{
sZipURL = getZIPURL(sReadLine,ziptag,sURL);
}
}
delete pRemoteURLFile;
pSessionURL.Close;

pSessionZip.OpenURL(sZipURL,1,INTERNET_FLAG_TRANSF ER_BINARY,NULL,NULL);
pRemoteZipFile =
pSessionZip.OpenURL(sZipURL,1,INTERNET_FLAG_TRANSF ER_BINARY,NULL,NULL);

//** The below line fails **
if( pLocalZipFile->Open(pFileName, CFile::modeCreate | CFile::modeWrite
|
CFile::typeBinary, &cFileErr ) )
{
while (pRemoteZipFile->Read(pbuf,100))
{
//count bytes read to see if size of file is correct
nBytesRead = nBytesRead + sizeof(pbuf);

//** write binary date to pLocalZipFile here **
}
printf("%d bytes read from remote file %s\n",nBytesRead,sZipURL);
delete pRemoteZipFile;
pSessionZip.Close;
return 0;
}
}

Jul 22 '05 #3
In the watch window after breaking it shows the following:

cFileErr {cause=0 OS Error=m_lOsError} CFileException

I've also tried to use a plain FILE* instead of a CSdtioFile for the
destination file to write locally using fopen(), fwrite() and fclose().
However this did produce a corrupt zip file of the incorrect size.
Somehow my file is larger than the zip if downloaded manually via IE.
Though I think this has to do with how I am reading the pRemoteZipFile.
"Jim Howard" <jn***@spamcop.net> wrote in message
news:Oj**************@TK2MSFTNGP15.phx.gbl...
What is coming back in cFileErr from
if( pLocalZipFile->Open(pFileName, CFile::modeCreate | CFile::modeWrite |
CFile::typeBinary, &cFileErr ) ) ?

Also, are your certain your app has write permission to pFileName?

Jim H
"David Jacques" <Da***********@ccrs.nrcan.gc.ca> wrote in message
news:cn*********@nrn2b.NRCan.gc.ca...
I have to write a console application which will fetch a zip on a daily
basis.
This zip file is hotlinked in a web page. The name of this .zip file will change every day. I've been able to start the session, open the URL,
read each line, and get the line with the .zip file URL in it no problem. Where I am running intro trouble is writing the zip file to disk as
binary.
I'm following the example from the VC++ Online Help reference to
CFile::Open(). Can anyone see what I am doing wrong ?

thanks in advance.

#include <afx.h>
#include <afxwin.h>
#include <afxinet.h>
#include <iostream>
#include <stdlib.h>

int main(int argc, _TCHAR* argv[])
{
CStdioFile* pRemoteURLFile;
CStdioFile* pRemoteZipFile;
CFile* pLocalZipFile;
CInternetSession pSessionURL("URLSession");
CInternetSession pSessionZip("ZipSession");
char* ziptag = "shapefile";
CString sURL;
CString sZipURL;
CString sReadLine;
char* pFileName = "test.dat";
int iFound;
int iNumLines;
UINT nBytesRead;
char pbuf[100];
CFileException cFileErr;
pRemoteURLFile = NULL;
pRemoteZipFile = NULL;
pLocalZipFile = NULL;
iNumLines = nBytesRead = 0;
sURL = "http://activefiremaps.fs.fed.us/fireptdata/modisfire_2004.htm"; pRemoteURLFile =
pSessionURL.OpenURL(sURL,1,INTERNET_FLAG_TRANSFER_ BINARY,NULL,NULL);
while (pRemoteURLFile->ReadString(sReadLine))
{
iNumLines ++;
iFound = sReadLine.Find("shapefile");
if (iFound > 0)
{
sZipURL = getZIPURL(sReadLine,ziptag,sURL);
}
}
delete pRemoteURLFile;
pSessionURL.Close;

pSessionZip.OpenURL(sZipURL,1,INTERNET_FLAG_TRANSF ER_BINARY,NULL,NULL); pRemoteZipFile =
pSessionZip.OpenURL(sZipURL,1,INTERNET_FLAG_TRANSF ER_BINARY,NULL,NULL);

//** The below line fails **
if( pLocalZipFile->Open(pFileName, CFile::modeCreate | CFile::modeWrite |
CFile::typeBinary, &cFileErr ) )
{
while (pRemoteZipFile->Read(pbuf,100))
{
//count bytes read to see if size of file is correct
nBytesRead = nBytesRead + sizeof(pbuf);

//** write binary date to pLocalZipFile here **
}
printf("%d bytes read from remote file %s\n",nBytesRead,sZipURL);
delete pRemoteZipFile;
pSessionZip.Close;
return 0;
}
}


Jul 22 '05 #4
David Jacques wrote:

I have to write a console application which will fetch a zip on a daily
basis.
This zip file is hotlinked in a web page. The name of this .zip file will
change every day. I've been able to start the session, open the URL,
read each line, and get the line with the .zip file URL in it no problem.
Where I am running intro trouble is writing the zip file to disk as binary.
I'm following the example from the VC++ Online Help reference to
CFile::Open(). Can anyone see what I am doing wrong ?
Why are you using so many pointers in your program?
CFile* pLocalZipFile;
This is a pointer to a CFile object
pLocalZipFile = NULL;
Here you set the pointer to 0.
//** The below line fails **
if( pLocalZipFile->Open(pFileName, CFile::modeCreate | CFile::modeWrite |
CFile::typeBinary, &cFileErr ) )


and here you program as if the pointer points to a CFile object, whih
of course is not the case.

WHy not simply

CFile LocalZipFile;

Now you have a CFile object

if( LocalZipFile.Open( pFileName, ....

and the CFile object will try to open the file.

Plain and simple:
Don't use pointers if you don't have to.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #5
Thanks to all who responded.
I solved the problem.
I just used a FILE* to create and write to the local file.
(Along with a 1 time read and write of bytes).
Zip file is the same as if manually downloading with a right click of the
mouse.
Not the most elegant solution, but it works.

<<snipped>>

pRemoteZipFile =
pSessionZip.OpenURL(sZipURL,1,INTERNET_FLAG_TRANSF ER_BINARY,NULL,NULL);
pFileName = getZipFileName(sZipURL);

if (( pLocalZipFile = fopen(pFileName,"wb")) != NULL)
{
//get # of bytes in whole file
dwActual = pRemoteZipFile->SeekToEnd();

//calculate size of buffer based on whole file size
pBuffer = (char*)calloc(sizeof(char),(dwActual));

//point file to beginning again to read
pRemoteZipFile->SeekToBegin();

//read whole file into this buffer once
lNumBytesRead = pRemoteZipFile->Read(pBuffer,dwActual);

//write the binary data locally all at once
fwrite(pBuffer,dwActual,1,pLocalZipFile);
}

fclose(pLocalZipFile);
delete pRemoteZipFile;
return 0;
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Arnold | last post by:
I need to read a binary file and store it into a buffer in memory (system has large amount of RAM, 2GB+) then pass it to a function. The function accepts input as 32 bit unsigned longs (DWORD). I...
3
by: Randy | last post by:
I have heard that access 2003 has functions for dealing with Long Binary Data. Does anyone know if this is true? Background: I am using 2000 with a table linked to a SQL server. One of the fields...
1
by: David Douthitt | last post by:
I have a binary statically linked application that was compiled against Red Hat 7.3 (presumably glibc 2.2.5). The application SegFaults when run in a Red Hat Advanced Server environment - with...
15
by: Foodbank | last post by:
Hi all, I'm trying to do a binary search and collect some stats from a text file in order to compare the processing times of this program (binary searching) versus an old program using linked...
4
by: Ken | last post by:
I have a binary tree in VB NET and insertions seem to be slow. The program receives data from one source and inserts it into the tree. The program receives data from another source and...
10
by: free2cric | last post by:
Hi, I have a single link list which is sorted. structure of which is like typedef struct mylist { int num; struct mylist *next;
1
by: asenthil | last post by:
Hai, i had tried to write a string which is fetched from a database into a file.... Here are my codings to write that fetched string into a text file... HANDLE hFile; DWORD wmWritten;...
5
by: Bhavesh | last post by:
Hello genious people, I m trying to insert a LARGE text from Multiline Textbox into my table of sqlserver2000. I m using vs-2005. Please note that I dont want to store blob data From FILE...
1
by: Bhavesh | last post by:
Hi Bruce, Thanks For Reply. U were right, Needed to pass string , but also need to pass size of Data( instead of 16, passed actual length of data). So that worked for me & didn't get any...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.