循环在C,循环问题

时间:2016-04-18 19:25:17

标签: loops

所以...,好吧我是初学者,所以不要太苛刻:)

我有这部分代码,我想一直执行。但是当我围绕它做一个while循环(或任何循环)时 - 它只是执行(因此循环因某些原因而无法工作)。问题是:我怎样才能产生一个常量这个代码循环?

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;
}

1 个答案:

答案 0 :(得分:0)

我通常做的无限循环是:

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

为了确保你的循环实际上是“循环”,只需创建一个随时增量的变量i,并将其打印出来:

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

如果print语句因为变量'i'是整数而给出错误,请查看以下链接: Converting int to string in c

修改

如果你的程序在没有进一步细节的情况下停止,你可能会出错。我会做的是:

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);
}

至少你得到一些输出。你可以找出错误所在的位置。

祝你好运: - )