My goal here is to download a file. And the theory is that if I read a Url and save it to a file, then technically that file was downloaded.
Code:
#include <stdio.h>
#include <windows.h>
#include <wininet.h>
#pragma comment (lib, "wininet.lib")
int main()
{
HINTERNET hOpen, hURL;
hOpen = InternetOpen("WebReader", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
hURL = InternetOpenUrl( hOpen, "http://www.google.com",
NULL, 0, 0, 0 );
const int numRead = 99;
char file[numRead];
unsigned long read;
do
{
InternetReadFile(hURL, file, numRead, &read);
file[read] = '\0';
printf("%s", file);;
} while (read == numRead);
printf ("\n");
return 0;
}
This program works fine, the problem is that when I try to save it to a file, it doesn't work, and I get bugs within the program, and a few warnings.
Code:
#include <stdio.h>
#include <windows.h>
#include <wininet.h>
#pragma comment (lib, "wininet.lib")
int main()
{
FILE *fp; /* Added */
HINTERNET hOpen, hURL;
hOpen = InternetOpen("WebReader", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
hURL = InternetOpenUrl( hOpen, "http://www.google.com",
NULL, 0, 0, 0 );
const int numRead = 99;
char file[numRead];
unsigned long read;
fp = fopen("c:\windows\desktop\whatever.txt", "w"); /* Added */
do
{
InternetReadFile(hURL, file, numRead, &read);
file[read] = '\0';
printf("%s", file);;
} while (read == numRead);
fclose(fp); /* Added */
printf ("\n");
return 0;
}
And with this code, for some reason the file doesn't open and it doesn't write the data to it. My goal is so that I can download more than just a text file. Say I would want a zip file, then I would use fopen to open a zip file and just write to it, and hopefully it will work like a zip file. I believe I am missing something really simple here, I would appreciate any help that point out my mistake.
Thank you!