0

I have this part of code that I want to execute all the time. But when I make a while loop (or any loop) around it - it simply executes (so the loop doesn't work for some reason). How can I make a constant loop for this code?

int main(void)
{
    int temp = 0;
    while (temp != 1) {       //here is the loop i was trying

        CURL *curl;
        CURLcode res;
        FILE *hd_src;
        struct stat file_info;
        curl_off_t fsize;

        struct curl_slist *headerlist = NULL;
        static const char buf_1[] = "RNFR " UPLOAD_FILE_AS;
        static const char buf_2[] = "RNTO " RENAME_FILE_TO;

        if (stat(LOCAL_FILE, &file_info)) {
            printf("Couldnt open '%s': %s\n", LOCAL_FILE, strerror(errno));
            return 1;
        }
        fsize = (curl_off_t)file_info.st_size;

        printf("Local file size: %" CURL_FORMAT_CURL_OFF_T " bytes.\n", fsize);

        hd_src = fopen(LOCAL_FILE, "rb");

        curl_global_init(CURL_GLOBAL_ALL);

        curl = curl_easy_init();
        if (curl) {
    
            headerlist = curl_slist_append(headerlist, buf_1);
            headerlist = curl_slist_append(headerlist, buf_2);
    
            curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);    
            curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); 
            curl_easy_setopt(curl, CURLOPT_URL, REMOTE_URL);
            curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
            curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
            curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
                (curl_off_t)fsize);
            
            res = curl_easy_perform(curl);
            
            if (res != CURLE_OK)

                fprintf(stderr, "curl_easy_perform() failed: %s\n",
                curl_easy_strerror(res));

            curl_slist_free_all(headerlist);

            curl_easy_cleanup(curl);
        }
        fclose(hd_src); 

        curl_global_cleanup();
    }

    return 0;
}
Kara
  • 6,115
  • 16
  • 50
  • 57
Raven
  • 1
  • 3

1 Answers1

0

What I usually do to make an endless loop is:

while(true)   // In C you can also write 'while(1)'
{
    // Your code
}

To be sure that your loop is actually 'looping', just make a variable i that increments all the time, and print it out:

int i = 0;
while(true)
{
    i++;
    print("Loop iteration: " + i);
    // Your code here.
}

If the print statement gives an error because the variable 'i' is an integer, check out this link: Converting int to string in c

EDIT

If your program just stops without further details, you probably have an error. What I would do is:

int i = 0;
while(true)
{
    i++;
    // Some code here
    print("I got to this point.");
    // Some more code
    print("Yet another milestone reached.");
    // Some more code
    print("The end of loop: " + i);
}

At least you get some output. And you can find out where the error is probably situated.

Good luck :-)

Community
  • 1
  • 1
K.Mulier
  • 8,069
  • 15
  • 79
  • 141
  • Good job! If you've got any more questions, don't hesitate to ask. I'm happy to help you out. Please push the "accept answer" button if it really helped you. That would be really nice ;-) – K.Mulier Apr 18 '16 at 20:59