C语言中的分段错误(代码转储)错误

C语言中的分段错误(代码转储)错误,c,file,terminal,segmentation-fault,void,C,File,Terminal,Segmentation Fault,Void,我试着运行这段代码,它说“分段错误(代码转储)”,我如何修复我的代码以消除这个错误。这是我的代码,如果有人能帮我,那就太棒了!如何修复我的无效搜索 #include <stdio.h> #include <stdlib.h> #include <string.h> struct _data { char *name; long number; }; int SCAN(FILE *(*stream)){ int count = 0;

我试着运行这段代码,它说“分段错误(代码转储)”,我如何修复我的代码以消除这个错误。这是我的代码,如果有人能帮我,那就太棒了!如何修复我的无效搜索

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

struct _data {
    char *name;
    long number;
};
int SCAN(FILE *(*stream)){
    int count = 0;
    char line[256];
    while (fgets(line, sizeof(line), *stream)) {
        count++;
    }
    return count;
}
struct _data*  BlackBoxLOAD(FILE **stream, int size){
    struct _data* BlackBox = (struct _data*)malloc(sizeof(struct _data)*size);
    int count = 0;
    char line[256];
    while (fgets(line, sizeof(line), *stream)) {
        char* token = strtok(line, " ");
        struct _data* temp = (struct _data*)malloc(sizeof(struct _data));
        temp->name = token;
        token = strtok(NULL, " ");
        temp->number = atoi(token);
        BlackBox[count] = *temp;
        count++;
    }
    return BlackBox;
}
void SEARCH(struct _data *BlackBox, char *string, int size){
    int i = 0;
    for (i = 0; i<size; i++){
        if (strcmp(BlackBox[i].name, string) == 0)
            return i;
    }
    return -1;
}
void FREE(struct _data *BlackBox, int size){
    free(BlackBox);
    return;
}
int main(int argc, char **argv) {
    int i = 0, size = 0;
    FILE *stream = fopen("hw5.data", "r");
    int noOfLines = SCAN(&stream);
    size = noOfLines;
    struct _data *BlackBox = BlackBoxLOAD(&stream, size);
    fclose(stream);
    for (i = 1; i<argc; i++){
        if (argv[i] == "") {
            printf("*******************************************");
            printf("* You must include a name to search for. *");
            printf("*******************************************");
        }
        int pos = SEARCH(BlackBox, argv[i], size);
        if (pos == -1) {
            printf("*******************************************");
            printf("The name was NOT found.");
            printf("*******************************************");
        }
        else{
            printf("*******************************************");
            printf("The name was found at the %d entry.", pos);
            printf("*******************************************");
        }
    }
    FREE(BlackBox, size);
}
#包括
#包括
#包括
结构数据{
字符*名称;
长数;
};
整数扫描(文件*(*流)){
整数计数=0;
字符行[256];
while(fgets(line,sizeof(line),*stream)){
计数++;
}
返回计数;
}
结构数据*Blackbox加载(文件**流,整数大小){
结构数据*黑盒=(结构数据*)malloc(sizeof(结构数据)*大小);
整数计数=0;
字符行[256];
while(fgets(line,sizeof(line),*stream)){
char*token=strtok(行“”);
结构数据*临时=(结构数据*)malloc(sizeof(结构数据));
temp->name=token;
令牌=strtok(空,“”);
temp->number=atoi(令牌);
黑盒[计数]=*温度;
计数++;
}
返回黑盒;
}
无效搜索(结构数据*黑盒、字符*字符串、整数大小){
int i=0;

对于(i=0;i在执行以下行之后

int noOfLines = SCAN(&stream);
位于文件末尾。在读取数据之前,您需要将其倒带。添加行:

frewind(stream);
调用
SCAN

另外,您应该将
SCAN
BlackBoxLOAD
的参数类型更改为
FILE*

int SCAN(FILE *stream){

struct _data*  BlackBoxLOAD(FILE *stream, int size){
这将使调用更容易,并且函数不必取消对
流的引用

功能定义:

int SCAN(FILE *stream){
    int count = 0;
    char line[256];
    while (fgets(line, sizeof(line), stream)) {
                                     // Just stream, not *stream

        count++;
    }
    return count;
}
函数调用:

int noOfLines = SCAN(stream);
                     // Just stream, not &stream.

这段代码中有太多的bug,但最明显的是:

    char* token = strtok(line, " ");
    struct _data* temp = (struct _data*)malloc(sizeof(struct _data)*1000);
    temp->name = token;
您正在将
temp->name
设置为
token
的值。但是
token
指向
,该行正在被修改,很快将不存在,因为您在即将消失的堆栈上创建了它


您无法保存指向要重用的内存块的指针。

您是否尝试在您喜爱的搜索引擎中插入“分段错误”?对于不知道seg错误是什么的人来说,这是一些有趣的复杂代码。它不是“代码转储”,而是“核心转储”,请参阅;因此基本上使用
gcc-Wall-Wextra-g编译程序,然后使用
gdb-yourprog-core