gpt4 book ai didi

c - 段错误打开文件

转载 作者:太空宇宙 更新时间:2023-11-04 06:06:04 25 4
gpt4 key购买 nike

我只是想开始学习 C http://fromlearncodethehardway.com .但是,在尝试从那里编写其中一个示例时,我遇到了段错误。 Here's我的源代码的链接。我尝试使用 valgrind 来进一步帮助调试,但我似乎无法弄清楚问题所在。这是来自 valgrind 的堆栈跟踪。

   ~/Chardway$ valgrind ./ex17 db.dat g
==4423== Memcheck, a memory error detector == 4423== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==4423== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info
==4423== Command: ./ex17 db.dat g
==4423==
==4423== Invalid read of size 1
==4423== at 0x4EA4F1D: _IO_file_fopen@@GLIBC_2.2.5 (fileops.c:271)
==4423== by 0x4E99DB5: __fopen_internal (iofopen.c:93)
==4423== by 0x400A76: Database_open (ex17.c:65)
==4423== by 0x400F7C: main (ex17.c:151)
==4423== Address 0x722b is not stack'd, malloc'd or (recently) free'd
==4423==
==4423==
==4423== Process terminating with default action of signal 11 (SIGSEGV)
==4423== Access not within mapped region at address 0x722B
==4423== at 0x4EA4F1D: _IO_file_fopen@@GLIBC_2.2.5 (fileops.c:271)
==4423== by 0x4E99DB5: __fopen_internal (iofopen.c:93)
==4423== by 0x400A76: Database_open (ex17.c:65)
==4423== by 0x400F7C: main (ex17.c:151)
.....

==4423== For counts of detected and suppressed errors, rerun with: -v
==4423== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 4)
Segmentation fault

任何有关调试的帮助都会很棒。谢谢!

最佳答案

在您的代码中,您将 fopen 调用为 fopen(filename,'w'); & fopen(filename,'r+');。这些应该是 fopen(filename,"w"); & fopen(filename,"r+");fopen的第二个参数应该是 const char *
希望这对您有所帮助!

关于c - 段错误打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10233631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com