C++ c++;执行结束时的段错误

C++ c++;执行结束时的段错误,c++,curl,libcurl,C++,Curl,Libcurl,我的代码进行编译,并且大部分执行它的目的,但是我在执行结束时遇到了一个段错误,它应该更新(追加)文件,但没有 #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,

我的代码进行编译,并且大部分执行它的目的,但是我在执行结束时遇到了一个段错误,它应该更新(追加)文件,但没有

#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: %s\n",
                        curl_easy_strerror(res));

        FILE * pFile;
        pFile = fopen ("myfile.txt","a+");
        if (pFile!=NULL)
        {
                curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, pFile);
                res = curl_easy_perform(curl);
                fclose (pFile);
        }

        /* always cleanup */
        curl_easy_cleanup(curl);
  }
  return 0;
}
#包括
#包括
大小写入数据(void*ptr、大小、大小nmemb、文件*流){
书写的大小;
写入=写入(ptr、大小、nmemb、流);
书面回报;
}
内部主(空)
{
卷曲*卷曲;
卷曲编码;
curl=curl_easy_init();
if(curl){
curl\u easy\u setopt(curl,CURLOPT\u URL,“http://pubapi.cryptsy.com/api.php?method=singlemarketdata&marketid=155");
/*执行请求,res将获得返回代码*/
res=旋度(curl)\u容易执行(curl);
/*检查错误*/
如果(res!=卷曲(OK)
fprintf(stderr,“curl\u easy\u perform()失败:%s\n”,
卷曲(容易的);
文件*pFile;
pFile=fopen(“myfile.txt”,“a+”);
if(pFile!=NULL)
{
curl\u easy\u setopt(curl,CURLOPT\u WRITEFUNCTION,pFile);
res=旋度(curl)\u容易执行(curl);
fclose(pFile);
}
/*总是清理*/
旋度\轻松\清洁(旋度);
}
返回0;
}
以下是gdb调试输出:

gdb /home/coinz/cryptsy/getprice.o /home/coinz/cryptsy/core
GNU gdb (Gentoo 7.8 vanilla) 7.8
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://bugs.gentoo.org/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /home/coinz/cryptsy/getprice.o...done.

warning: exec file is newer than core file.
[New LWP 665]

warning: Could not load shared library symbols for linux-vdso.so.1.
Do you need "set solib-search-path" or "set sysroot"?
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
Core was generated by `./getprice.o'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00000000011e12a0 in ?? ()
(gdb) bt full
#0  0x00000000011e12a0 in ?? ()
No symbol table info available.
#1  0x00007f9e78b9ca48 in ?? () from /usr/lib64/libcurl.so.4
No symbol table info available.
#2  0x00007f9e78bb4cd0 in ?? () from /usr/lib64/libcurl.so.4
No symbol table info available.
#3  0x00007f9e78bb0a4a in ?? () from /usr/lib64/libcurl.so.4
No symbol table info available.
#4  0x00007f9e78bb8fe8 in ?? () from /usr/lib64/libcurl.so.4
No symbol table info available.
#5  0x00007f9e78bb9e15 in curl_multi_perform () from /usr/lib64/libcurl.so.4
No symbol table info available.
#6  0x00007f9e78bb22d6 in curl_easy_perform () from /usr/lib64/libcurl.so.4
No symbol table info available.
#7  0x0000000000400a76 in main () at getprice.cpp:35
        pFile = 0x11e12a0
        curl = 0x11ce2c0
        res = CURLE_OK
(gdb)
gdb/home/coinz/cryptsy/getprice.o/home/coinz/cryptsy/core
GNU gdb(Gentoo 7.8香草)7.8
版权所有(C)2014免费软件基金会。
许可证GPLv3+:GNU GPL版本3或更高版本
这是自由软件:您可以自由更改和重新发布它。
在法律允许的范围内,不存在任何担保。键入“显示复制”
和“显示保修”了解详细信息。
此GDB配置为“x86_64-pc-linux-gnu”。
键入“显示配置”以获取配置详细信息。
有关错误报告说明,请参阅:
.
在线查找GDB手册和其他文档资源,网址为:
.
要获得帮助,请键入“帮助”。
键入“apropos word”以搜索与“word”相关的命令。。。
从/home/coinz/cryptsy/getprice.o读取符号…完成。
警告:exec文件比核心文件新。
[新LWP 665]
警告:无法加载linux vdso.so.1的共享库符号。
您需要“设置solib搜索路径”还是“设置系统根”?
[已启用使用libthread_db的线程调试]
使用主机libthread_db library“/lib64/libthread_db.so.1”。
核心是由“./getprice.o”生成的。
程序以信号SIGSEGV终止,分段故障。
#0 0x00000000011e12a0英寸??()
(gdb)英国电信全部
#0 0x00000000011e12a0英寸??()
没有可用的符号表信息。
#1 0x00007f9e78b9ca48英寸??()来自/usr/lib64/libcurl.so.4
没有可用的符号表信息。
#2 0x00007f9e78bb4cd0英寸??()来自/usr/lib64/libcurl.so.4
没有可用的符号表信息。
#3 0x00007f9e78bb0a4a英寸??()来自/usr/lib64/libcurl.so.4
没有可用的符号表信息。
#4 0x00007f9e78bb8fe8英寸??()来自/usr/lib64/libcurl.so.4
没有可用的符号表信息。
#来自/usr/lib64/libcurl.so.4的curl_multi_perform()中的5 0x00007f9e78bb9e15
没有可用的符号表信息。
#来自/usr/lib64/libcurl.so.4的curl\u easy\u perform()中的6 0x00007f9e78bb22d6
没有可用的符号表信息。
#getprice.cpp:35处的main()中的7 0x0000000000400a76
pFile=0x11e12a0
旋度=0x11ce2c0
res=卷曲(正常)
(gdb)

以下行是一个错误:

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, pFile);
CURLOPT_WRITEFUNCTION选项需要一个函数指针,但需要传递文件处理程序。此外,您从未告诉libcurl使用write_数据函数

您应该同时设置写入函数和写入数据选项

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, pFile);
有关更多详情,请访问:


您是否尝试过在调试器中运行代码以查看其实际停止的位置?然后加载pFile时可能会出现问题,因为我没有看到定义的路径,只有名称字符串。程序会获取网页,将所有内容输出到缓冲区,但最后的字符是“段错误”,并且文件中正确填充了数据。但是,在第二次执行时,我希望文件大小是原来的两倍,但大小是一样的……,{“price”:“0.00493171”,“quantity”:“30.41540139”,“total”:“0.1499994”},{“price”:“0.00493000”,“quantity”:Segmentation faultgdb output->