This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center
6年前关闭。
我正在将文件加载到内存中,并使用以下语句执行此操作:
        if ((ch = fread(&temp[i],1,1,loadDict)) == EOF)
            break;

但我收到了一个分割错误在使用gdb进行检查时,我确认故障发生在此行(if语句,在中断之前)为什么它看不到它会出错(使用EOF的整个要点)?
我想可能是在if语句中而不是在while()语句中使用了EOF是否可以在if语句中使用EOF?
更新:更多代码
bool load(const char* dictionary)
{
FILE* loadDict = fopen(dictionary, "r");

char* new = malloc(sizeof(char)); // for storing the "new-line" character
*new = 0x0A;

// defines a node, which containes one word and points to the next word
typedef struct node
{
char* word;
struct node* next;
}
node;


    node* head = malloc(sizeof(node));
    node* temp = malloc(sizeof(node));
    head->next=temp;

    // copies a word from the dictionary file into a node

    int* numStore = malloc(sizeof(int)); //count for number of words in dictionary
    int num = 0;
    int ch = 0; // to hold for EOF
    int flag = 0; // for breaking out of while loop
    while(true)
    {
    node* newNode = malloc(sizeof(node));
    temp->next=newNode;
    temp->word=malloc(46);

        int i = -1;

        do
        {
        i++;
        if (!feof(loadDict) || !ferror(loadDict))
        {
            flag = 1;
            break;
        }
        fread(&temp[i],1,1,loadDict);

        if  (memcmp (new, &temp[i], 1) == 0)
            num += 1;
        }
        while(memcmp (new, &temp[i], 1) != 0);

    temp=newNode;

    if (flag == 1)
        break;
    }

   numStore = #

return true;
}

最佳答案

这是编译的…我现在也运行它了分配失败时的错误处理是应该受到谴责的;它至少应该给出一条错误消息,并且可能应该释放所有分配的节点并从函数返回0(NULL)(并关闭文件)。

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

typedef struct Node
{
    char        *word;
    struct Node *next;
} Node;

Node *dict_load(const char *dictionary)
{
    FILE *loadDict = fopen(dictionary, "r");

    if (loadDict == 0)
        return 0;

    Node *head = 0;
    char line[4096];
    while (fgets(line, sizeof(line), loadDict) != 0)
    {
        size_t len = strlen(line);           // Includes the newline
        Node *node = malloc(sizeof(*node));
        if (node == 0)
            exit(1);    // Reprehensible
        node->word = malloc(len);
        if (node->word == 0)
            exit(1);    // Reprehensible
        memmove(node->word, line, len - 1);  // Don't copy the newline
        node->word[len-1] = '\0';            // Null terminate the string - tweaked!
        node->next = head;
        head = node;
    }

    fclose(loadDict);
    return head;
}

如果必须从函数返回bool,则可能需要:
static bool dict_load(const char *dictionary, Node **head)

如果参数列表仅固定在文件名上,那么您将被迫使用一个全局变量,这对设置练习的人来说是很糟糕的。这是“可行的”,但“丑陋如罪”。
上面的代码确实可以工作(请注意调整后的行);添加函数dict_free()dict_print()来发布字典并打印字典,再加上在dict_load()中进行正确的错误处理,一个简单的main()允许我在自己的源代码上测试它,并且它可以工作(向后打印源代码)它也能从valgrind得到一份健康的账单。

关于c - 即使在使用EOF时C分割错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15962446/

10-11 22:05
查看更多