Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
您确定调用此代码并且“模型”是 1、2 或 3 吗? 如果没有, fptr3 将不会被设置,所以如果你尝试用它做任何事情,你就会遇到麻烦。
试试这个?:
附加说明。 请,请,请考虑一致的支架样式! :)
Are you sure this code is called and "model" is either 1, 2, or 3? If not, fptr3 would not be set, so if you try to do anything with it you'd be in trouble.
Try this instead?:
An additonal note. Please, please, please consider consistent brace styles! :)
这是C#吗???? 哇,别开玩笑了,你遇到了段错误......
你知道返回码的用途吗???
在寻求此类帮助之前,您确实需要改进代码......这确实是不可读且丑陋的代码......
开始对文件指针添加一些检查
......
Is this C# ???? wooow no kidding you are getting segfaults...
Do you know what return codes are for ???
You really need to improve your code before asking for this kind of help....this is really unreadable, and ugly code...
Start adding some check to your file pointers
...
valgrind
是 C 语言中段错误的最佳补救措施。它可以发现错误在段错误之前,并准确地告诉您做错了什么。 为了获得最大收益,请在打开调试符号的情况下进行编译。valgrind
is the sovereign remedy for segfaults in C. It finds errors before the segfault and tells you exactly what you did wrong. For maximum benefit, compile with debugging symbols turned on.我同意丹尼尔的观点,你需要检查你的返回值和指针。
我也同意您需要更好地阻止和格式化代码。 假设你是唯一读过这本书的人,那么几周后,你甚至会忘记得足够多,以至于无法跟上它。
对于马修斯的观点,您可以尝试删除以以下行开头的所有内容:
当然,只有在检查文件指针后才需要处理此问题。
另一个疯狂的想法是使用调试器单步执行它。 我知道当您有像 printf 语句这样出色的调试选项时,很难使用调试器,但有时调试器可以节省大量时间(例如,当您不断出现段错误时)。 此外,学习如何使用您正在开发的平台的调试器是将优秀程序员与普通黑客区分开来的因素之一。 (仅供参考,您甚至可以尝试在调试器中查看核心,也许可以准确地看出问题出在哪里。(提示:如果您没有核心文件,请尝试“man ulimit”。))
如果您要坚持使用 printf 作为唯一的调试方法,然后在每次使用后立即调用刷新。
I agree with Daniel, you need to be checking your return values and pointers.
I also agree that you need to do a better job blocking and formatting your code. Supposing that you are the only one that ever reads it, in a couple of weeks even you will have forgotten enough that you won't be able to follow it.
Towards Matthews point, you could try deleting everything starting with the line:
Of course, only bother with this after checking the file pointer.
Another crazy idea would be stepping through it with a debugger. I know it's hard to use a debugger when you have such great debugging options as the printf statement, but sometimes a debugger can be a huge time saver (for example, when you keep getting segfault). Also, learning how to use the debugger for the platform you are developing on is one of the things that separates good coders from the pack of average hacks. (FYI, you could even try looking at your core in the debugger and maybe see exactly where the problem is. (Hint: if you don't have a core file try 'man ulimit'.))
If you are going to insist on using printf as your only means of debug, then call flush right after each use.
freq 定义为什么? 概率定义为什么?
第一个 for 循环是否只针对频率运行 fscanf ? 或者它应该涵盖整个街区?
编辑:基本原理 - 如果您看到的只是 5a,那么它在 freq 上的 fscanf 中出现故障。 您可能为 freq 分配的空间太少,或者使用了不正确的类型。
What's freq defined as? What's prob defined as?
Is the first for loop meant to only run fscanf for freq? Or is it supposed to encompass the entire block?
EDIT: Rationale - If all you see is 5a, then it's faulting in the fscanf on freq. You may have allocated too little space for freq, or used an incorrect type.
检查返回代码,并在需要时使用 perror() 打印文件的错误代码(请参阅丹尼尔的回答)。 使用 FILE * 为 NULL 调用 fscanf() 肯定会出现段错误。
正如您指定的 %lf,freq 必须是至少包含 20 个双精度数的数组,而不仅仅是浮点数。
请参阅 man ffrintf()。
使用调试器代替 printf()。
Check the return code and print if needed the error code of your files with perror() (see. daniel answer). Calling fscanf() with a FILE * to NULL, will surely do a segfault.
As you specified %lf, freq must be an array of at least 20 doubles, not just float.
See man ffrintf().
use a debugger instead of printf().
您应该发布正在打开的任何文件的开头内容,因为它在 fscanf 中崩溃了。 该文件要么不存在,要么在与您尝试解析它的方式堆叠时格式错误。
You should post the contents of the beginning of whatever file is getting opened since it's crashing in fscanf. Either the file doesn't exist, or it's malformed when stacked against to how you are trying to parse it.