简体   繁体   English

写入文件时出现分段错误

[英]Segmentation fault when writing to a file

UPDATED CODE FULLY FUNCTIONAL***更新后的代码功能齐全***

I'm new to C/C++ and I wrote a piece of code that would count the number of times the program has run by writing 'count' to a file each time.我是 C/C++ 的新手,我写了一段代码,通过每次将“count”写入文件来计算程序运行的次数。 Except I'm getting a seg fault error when I try to run it.除了当我尝试运行它时遇到段错误错误。 Anyone can tell me why ?谁能告诉我为什么?

#include <stdlib.h>
#include <stdio.h>

int main(int argc, const char * argv[]) {
    FILE *fp;
    printf("LINE 13 CLEAR");
    fp = fopen("MyPlayground.rtf", "r");
    int value;
    fscanf(fp, "%d", &value);
    printf("Value is %d", value);
    //printf("LINE 14 CLEAR");
    if(fp== NULL)
    {
        printf("LINE 17 CLEAR");
        if(fopen("MyPlayground.rtf", "w")==NULL)
        {
            exit(0);
        }
        //fprintf(fp,"%d",0);
        fclose(fp);
        return 0;
    }
    //printf("LINE 25 CLEAR");
    fp = fopen("MyPlayground.rtf", "w");
    //printf("LINE 30 CLEAR");
    fprintf(fp,"%d",++value);
    fclose(fp);
}

EDIT: The program runs until "printf("LINE 17 CLEAR");"编辑:程序运行直到“printf(“LINE 17 CLEAR”);” and after that it gives me an 'EXC-BAD-ACCESS' error in the following line.之后,它在以下行中给我一个“EXC-BAD-ACCESS”错误。

EDIT1: Fixed fixed the two different paths EDIT1:修复了两条不同的路径

EDIT2: Added error check for second fopen(...) statement. EDIT2:为第二个fopen(...)语句添加了错误检查。

Also, feel free to comment on any style errors and things I should/should not do.另外,请随时对任何样式错误和我应该/不应该做的事情发表评论。 Thanks.谢谢。

I bet it only happens a first time you run it.我敢打赌它只会在您第一次运行时发生。 The reason why is line 11:原因是第 11 行:

11 fprintf(fp, 0); 11 fprintf(fp, 0);

You are basically passing fprintf a null pointer.您基本上是向 fprintf 传递一个空指针。 What you probably meant to do was this:你可能想要做的是:

11 fprintf(fp, "0"); 11 fprintf(fp, "0");

Here is how I figured it out.这是我想出来的方法。 I compiled it with debug info and ran it using debugger... lldb, to be precise:我用调试信息编译它并使用调试器运行它...... lldb,准确地说:

(lldb) bt * thread #1: tid = 0xacaaf, 0x00007fff90f4dcf6 libsystem_c.dylib __vfprintf + 327, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0) * frame #0: 0x00007fff90f4dcf6 libsystem_c.dylib __vfprintf + 327 (lldb) bt * thread #1: tid = 0xacaaf, 0x00007fff90f4dcf6 libsystem_c.dylib __vfprintf + 327, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0) * frame #0: 0x00007fff90f4dcf6 libsystem_c.dylib __vfprintf + 327

frame #1: 0x00007fff90f762e7 libsystem_c.dylib`__v2printf + 471

frame #2: 0x00007fff90f766bc libsystem_c.dylib`__xvprintf + 633

frame #3: 0x00007fff90f4db36 libsystem_c.dylib`vfprintf_l + 54

frame #4: 0x00007fff90f4669b libsystem_c.dylib`fprintf + 186

frame #5: 0x0000000100000e9d test.out`main(argc=1, 

argv=0x00007fff5fbffc60) + 109 at test.c:11 frame #6: 0x00007fff8f9c95fd libdyld.dylib`start + 1 argv=0x00007fff5fbffc60) + 109 at test.c:11 frame #6: 0x00007fff8f9c95fd libdyld.dylib`start + 1

...if you look at frame 5, you will see the offending line of code. ...如果您查看第 5 帧,您将看到有问题的代码行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM