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

时间:2015-03-02 22:24:38

标签: 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;
    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);
}

2 个答案:

答案 0 :(得分:0)

执行以下行后

int noOfLines = SCAN(&stream);

stream位于文件的末尾。在读取数据之前,您需要回放它。添加以下行:

frewind(stream);

致电SCAN后。

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

int SCAN(FILE *stream){

struct _data*  BlackBoxLOAD(FILE *stream, int size){

这样可以使通话更轻松,功能也不必取消引用stream

功能定义:

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.

答案 1 :(得分:0)

此代码中存在太多错误,但这里最明显:

    char* token = strtok(line, " ");
    struct _data* temp = (struct _data*)malloc(sizeof(struct _data)*1000);
    temp->name = token;

您将temp->name设置为token的值。但是token指向line,它正在被修改,很快就会停止存在,因为你是在即将离开的堆栈上创建的。

您无法将指针保存到您即将重复使用的一块内存中。