c++如何在CURLOPT_中写入一个换行符

c++ how to write a newline into CURLOPT_

本文关键字:换行符 一个 CURLOPT c++      更新时间:2023-10-16

我已经尝试了所有显而易见的方法,在CURLOPT_流的循环结束时,在文件末尾写一行换行符。我没有收到错误,但也从来没有写过换行符。如何在CURLOPT_中插入新行?

#include <iostream>
#include <stdio.h>
#include <curl/curl.h>

size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
        size_t written;
        written = fwrite(ptr, size, nmemb, stream);
        return written;
}

int main(void)
{
        CURL *curl;
        CURLcode res;
        curl = curl_easy_init();
        if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "http://pubapi.cryptsy.com/api.php?method=singlemarketdata&marketid=155");
        /* 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: %sn",
                        curl_easy_strerror(res));
        FILE * pFile;
        pFile = fopen ("/home/coinz/cryptsy/myfile.txt","a+");
        if (pFile!=NULL)
        {
                curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
                curl_easy_setopt(curl, CURLOPT_WRITEDATA, pFile);
                res = curl_easy_perform(curl);
                std::cout << pFile << std::endl;
                //pFile << "nr";
                fclose (pFile);
        }
        /* always cleanup */
        curl_easy_cleanup(curl);
  }
  return 0;
}

尝试fprintf(pFile, "n");而不是<<运算符。