C Language Tutorial: Downloading Music397


In this tutorial, we will learn how to download music using the C programming language. We will use the libcurl library, which is a popular and powerful library for transferring data over the Internet. We will first create a simple program that downloads a file from a URL. Then, we will extend this program to download multiple files from a list of URLs.

Prerequisites

Before we start, you will need to have the following installed on your system:* A C compiler
* The libcurl library

On most Linux distributions, you can install libcurl by running the following command:```bash
sudo apt-get install libcurl4-dev
```

On macOS, you can install libcurl using Homebrew:```bash
brew install libcurl
```

On Windows, you can download libcurl from the official website:/download/

A Simple Download Program

Let's start by creating a simple program that downloads a file from a URL. Here is the code:```c
#include
#include
#include
int main(int argc, char *argv[]) {
// Check if the user has provided a URL
if (argc != 2) {
fprintf(stderr, "Usage: %s ", argv[0]);
return EXIT_FAILURE;
}
// Initialize libcurl
curl_global_init(CURL_GLOBAL_ALL);
// Create a new curl handle
CURL *curl = curl_easy_init();
if (!curl) {
fprintf(stderr, "curl_easy_init() failed");
return EXIT_FAILURE;
}
// Set the URL to download
curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
// Set the file to save the downloaded data to
curl_easy_setopt(curl, CURLOPT_WRITEDATA, stdout);
// Perform the download
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s", curl_easy_strerror(res));
return EXIT_FAILURE;
}
// Cleanup libcurl
curl_easy_cleanup(curl);
curl_global_cleanup();
return EXIT_SUCCESS;
}
```

To compile this program, run the following command:```bash
gcc download.c -lcurl -o download
```

You can then run the program by passing the URL of the file you want to download as an argument:```bash
./download /
```

This will download the file and save it to the current directory.

Downloading Multiple Files

We can extend the above program to download multiple files from a list of URLs. Here is the code:```c
#include
#include
#include
// Define the maximum number of files to download
#define MAX_FILES 10
// Define the structure for storing the download information
typedef struct {
char *url;
FILE *fp;
} download_info;
// Write the downloaded data to a file
size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream) {
download_info *info = (download_info *)stream;
return fwrite(ptr, size, nmemb, info->fp);
}
int main(int argc, char *argv[]) {
// Check if the user has provided at least one URL
if (argc < 2) {
fprintf(stderr, "Usage: %s ... ", argv[0]);
return EXIT_FAILURE;
}
// Initialize libcurl
curl_global_init(CURL_GLOBAL_ALL);
// Create a new curl handle
CURL *curl = curl_easy_init();
if (!curl) {
fprintf(stderr, "curl_easy_init() failed");
return EXIT_FAILURE;
}
// Set the maximum number of simultaneous downloads
curl_easy_setopt(curl, CURLOPT_MAXCONNECTS, MAX_FILES);
// Create an array of download information structures
download_info info[MAX_FILES];
// Open the files to save the downloaded data to
for (int i = 0; i < argc - 1; i++) {
info[i].url = argv[i + 1];
info[i].fp = fopen(argv[i + 1], "wb");
if (!info[i].fp) {
fprintf(stderr, "fopen() failed for %s", argv[i + 1]);
return EXIT_FAILURE;
}
}
// Set the callback function for writing the downloaded data
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
// Set the user data for the callback function
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &info);
// Perform the downloads
CURLM *multi = curl_multi_init();
if (!multi) {
fprintf(stderr, "curl_multi_init() failed");
return EXIT_FAILURE;
}
for (int i = 0; i < argc - 1; i++) {
// Set the URL to download
curl_easy_setopt(curl, CURLOPT_URL, info[i].url);
// Add the handle to the multi handle
curl_multi_add_handle(multi, curl);
}
int running;
do {
curl_multi_perform(multi, &running);
} while (running);
// Cleanup libcurl
curl_multi_cleanup(multi);
curl_easy_cleanup(curl);
curl_global_cleanup();
// Close the files
for (int i = 0; i < argc - 1; i++) {
fclose(info[i].fp);
}
return EXIT_SUCCESS;
}
```

To compile this program, run the following command:```bash
gcc download_multiple.c -lcurl -o download_multiple
```

You can then run the program by passing the URLs of the files you want to download as arguments:```bash
./download_multiple / / /
```

This will download the three files and save them to the current directory.

2024-11-23


Previous:How to Draw a Hummingbird: A Step-by-Step Guide for Aspiring Artists

Next:The Ultimate Guide to Mastering Finally