在 CURL 中打开文件

File opening in CURL

我正在使用 libcurl 编写一个 c 程序。我正在连接到本地主机上的服务器 运行,然后将从服务器检索到的数据保存在一个文件中。当我使用 "file.txt" 这样的文件名时,我就可以读写我的文件了。但是,当我尝试更改文件名并将其附加到当前数据和时间时,出现错误 "Failed Writing received data to disk/application"。

以下是我的代码:

`    int main(int argc, char *argv[])
    { 
      float freq;
      CURL *curl;
      CURLcode res;
      FILE *fp, *file_r;
      char *buffer;
      buffer = malloc (100000);
      char url[1024], name[20];
      char *filename;
      filename = (char *) malloc (100);
      if (filename == NULL)
      {
        printf("Error in memory allocation");
      }`

  /* Store the data retireved from webpage in a file and name the file  with current date and time */
      time_t  now;
      time (&now);
      struct tm *t = localtime (&now);
      strftime (name, 20, "%d %m %Y %H:%M:%S", t);
  //  printf ("%s\n", name);

      filename[0] ='[=10=]'; // Ensure the memory is an emtpy string;
      strcat (filename, "file_new_");
      strcat (filename, name);
      strcat (filename, ".txt");
      printf ("%s\n", filename);
      printf("enter your input frequency value\n");
      scanf("%f", &freq);
      sprintf(url, "http://127.0.0.1:8080/?action=update_frequency&update_frequency=%f",freq);

    /* In windows, this will init the winsock stuff */
      curl_global_init(CURL_GLOBAL_ALL);

    /* get a curl handle */
      curl = curl_easy_init();
      if(curl)
      {
          curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1:8080");

          /* Now specify the username & pwd */

          curl_easy_setopt(curl, CURLOPT_USERPWD, "admin:12345");

          /* Specify the web request */

          curl_easy_setopt(curl, CURLOPT_URL, url);

          /* Storing the received data in a file */

          fp = fopen (filename, "w+");

          curl_easy_setopt (curl, CURLOPT_WRITEDATA, fp);

    /* Perform the request, res will get the return code */

          res = curl_easy_perform(curl);

        /* Check for errors */
          if (res != CURLE_OK)
          {
            fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
          }

          fclose (fp);

    /* To read the return msg from server after performing a request */

          file_r= fopen (filename, "r");
          fseek (file_r, SEEK_SET, 0);
          while (fgets (buffer, 100000, file_r)!=NULL)
          {
            ;
          }
          printf( "%s\n", buffer);
          fclose (file_r);
          }

        /* always cleanup */
          curl_easy_cleanup(curl);

          curl_global_cleanup();
          return 0;
        }

如果有人有任何想法,将会有很大帮助。 非常感谢

您不能在 Windows 上的文件名中使用 : 字符,因此无法保存。将分隔符更改为其他内容。