libCurl...AAAAAAHHHHHH!!!

I can't get a simple ftp upload to work to save my life. The file is created on my server, but it is always blank (0 bytes). I have no idea why.

Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <curl/curl.h>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

string data;

size_t upload(void* buf, size_t size, size_t nmemb, void* p){
    cout << "Called..." << endl;
    
    for (int c = 0; c<size*nmemb; c++){
        if (data[data.size()]==((char*)buf)[c])
        return 0;
        
        ((char*)buf)[c] = data[c];}
    return size*nmemb;}

int main(){
    cout << "Type a string to be uploaded: ";
    getline(cin, data);
    cout << data;
    
    CURL* curl;
    
    curl_global_init(CURL_GLOBAL_ALL);
    curl = curl_easy_init();
    
    curl_easy_setopt(curl, CURLOPT_URL, "ftp://USERNAME:PASSWORD@DOMAIN.com/file.txt");
    curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
    curl_easy_setopt(curl, CURLOPT_READFUNCTION, &upload);
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

    curl_easy_perform(curl);

    cin.get();
    return 0;}


I believe that the problem may be with my readfunction, but I don't know what else it would have to have to work. Any help would be GREATLY appreciated. Thanks!
I'm not sure what you meant to do on lines 13-14, but if a is an std::string, a[a.size()] is always zero, so if libcurl initialized buf to zero, the function will always return immediately.
I also tried if (c>=data.size()), but that doesn't work either. size*nmemb is the largest amount of data the buffer can hold, and "c" is in a for loop, up to size*nmemb. If c is greater than the data string's size, then I have reached the end and return 0. Else, I return the number of bytes I "took care of" (size*nmemb).

No matter what I do, the file ALWAYS ends up blank. Its driving me nuts!!!!!!!!!!!
Check out CURLOPT_INFILESIZE. Next time, RTM.
curl_easy_setopt(curl, CURLOPT_INFILESIZE, data.size());

Is this correct? Whenever I use it, the file doesn't get created at all. When I don't use it, the file turns out blank. Any ideas?
None. See if copying from the tutorial on uploading helps.
GOT IT WORKING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

For some reason, returning 0 on the first call of the callback will signal some type of error and libcurl won't upload it. I had to return (c-1) and use a bool flag to have the callback get called 2x (returning 0 the 2nd time) to get it to work. If you return 0 on the first time, it doesn't work, and if you don't return 0, the callback is called indefinitely. The error buffer said "unaligned blah blah blah". I'm assuming that was because of me returning incorrect numbers at first.

Thanks helios!!
Topic archived. No new replies allowed.