簡體   English   中英

C11 總線錯誤:10

[英]C11 Bus error:10

我是 C 的新手,我目前只是想讀取一個內容為“6”的文件,沒有別的。 每當我運行文件時,我都會得到:總線錯誤:10。

#include <stdio.h>
#include <stdlib.h>
char input(void);

int main(int argc, char** argv)
{


    input();

    return (EXIT_SUCCESS);
}

char input(void)
{
    FILE *fp;
    char *score;

    fp = fopen("data.bin", "rt");


    fscanf(fp,"%s", score);

    printf("%s", score);

    fclose(fp);

}

我像這樣修改了你的代碼:

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

void input(void);

int main(int argc, char** argv) {
    input();
    return(EXIT_SUCCESS);
}

void input(void) {
    char buffer[10];
    FILE *ptr;
    ptr = fopen("data.bin","rb");  // r for read, b for binary
    fread(buffer, sizeof(buffer), 1, ptr); // read 10 bytes to our buffer
    printf("%s", buffer);

    fclose(ptr);
}

輸出:

6

更多信息,請閱讀: Read/Write to binary files in C

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM