#include #include #include #pragma comment(lib, "wininet.lib") int main() { HINTERNET hInternet, hConnect; // 初期化 hInternet = InternetOpen(L"FileDownloadExample", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0); if (hInternet == NULL) { printf("InternetOpen failed\n"); return 1; } // ファイルをダウンロードするサイトのURL LPCWSTR url = L"https://www5e.biglobe.ne.jp/~develop/c/CharIsNumber.c"; // 接続を開く hConnect = InternetOpenUrl(hInternet, url, NULL, 0, INTERNET_FLAG_RELOAD, 0); if (hConnect == NULL) { printf("InternetOpenUrl failed\n"); InternetCloseHandle(hInternet); return 1; } // ファイルを保存するためのハンドルを作成 HANDLE hFile = CreateFile(L"downloaded_file.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { printf("CreateFile failed\n"); InternetCloseHandle(hConnect); InternetCloseHandle(hInternet); return 1; } // ダウンロードしたデータをファイルに書き込む char buffer[1024]; DWORD bytesRead; while (InternetReadFile(hConnect, buffer, sizeof(buffer), &bytesRead) && bytesRead > 0) { WriteFile(hFile, buffer, bytesRead, NULL, NULL); } // ハンドルを閉じる CloseHandle(hFile); InternetCloseHandle(hConnect); InternetCloseHandle(hInternet); printf("File downloaded successfully.\n"); return 0; }