ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello!
I'm new to libcurl and quite newbie to C++. I'm trying to undestand how to send files to a FTP remote host.
I basically understood the example provided with libcurl-dev package (/usr/share/doc/libcurl-devel-7.18.2/ftpupload.c).
Trying to implement libcurl with my C++ program I found difficults.
As it is said in the libcurl doc, to implement Easy API with C++ requires some modifications:
libcurl with C++
There's basically only one thing to keep in mind when using C++ instead of C when interfacing libcurl:
The callbacks CANNOT be non-static class member functions
Example C++ code:
class AClass {
static size_t write_data(void *ptr, size_t size, size_t nmemb,
void *ourpointer)
{
/* do what you want with the data */
}
}
Does anybody knows where I can get a C++ basic example on how to use libcurl's easy API to upload files?
Your libcurl package should have come with an examples directory, have you looked there?
ftpupload.c is a good place to start.
As for the static callback, it just means that any callback function that you pass to curl must be declared as a static member function if it is encapsulated in a class.
That prevents moveable classes from being yanked out from under the write process.
Global functions would work just as well, if you didn't want to make the callback a member function.
Other than that, I think all of the curl_easy* functions are the same in C as C++.
If there's something there that isn't clear to you, please ask...
Thanks for your answer Raconteur.
I left my example code at job. But I'll see if I can try to write uploadftp program in C++ this weekend at home. I'll let you know my results or doubts...
My guide was g++ warnings / errors. I found that I had to do some little change in the callback function. This way:
Code:
class UploadFtp
{
static size_t read_callback(void *ptr, size_t size, size_t nmemb, FILE* stream)
{
/* in real-world cases, this would probably get this data differently
as this fread() stuff is exactly what the library already would do
by default internally */
size_t retcode = fread(ptr, size, nmemb, stream);
fprintf(stderr, "*** We read %d bytes from file\n", retcode);
return retcode;
}
private:
char user [20];
char pwd [20];
char remote_url [128];
char local_file [128];
char remote_filename [128];
public:
(...)
Based on g++ I changed the void pointer "void * stream" to "FILE * stream". Code is working. But I'd like to know more details on how the callback function works and why is needed? Do you have some clue where I can find more info about it?
The stream pointer in the default implementation is declared as a void pointer to allow it to be cast as necessary. It could be an ostream class or file descriptor, etc. Your implementation is good, there are many different ways to do it.
When you make a call to a function in a dynamically linked library, it is sometimes necessary for that library to "call back" into the calling application.
In this case, libcurl needs to know what to do with the data it is recieving or transmitting (where and how to store it or where and how to get it).
The library needs that to be a rather open interface to be flexible, so it provides a framework for the library to call back to user-defined functions.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.