c - strcmp() 来自标准输入的字符串和来自文件的字符串

标签 c string list file strcmp

<分区>

我在比较文件中的字符串时遇到问题。 我想从一个字典文件中创建一个单词列表。我不知道为什么 strcmp() 只返回 -1 或 1,即使我使用文件中的单词也是如此。在输出中我有例如:1somethingsomething 而不是 0somethingsomething

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


struct words
{
    char *word;
    struct words *next;
};

void pushBack(struct words **headPointer, char string[])
{
    struct words *pointer;
    pointer = *headPointer;
    if (*headPointer == NULL)
    {

        *headPointer = (struct words*)malloc(sizeof(struct words));
        (*headPointer)->next = NULL;
        (*headPointer)->word = (char*)malloc(sizeof(char)*(strlen(string)+1));
        strcpy((*headPointer)->word, string);

    }
    else
    {
        while (pointer->next != NULL)
        {
            pointer = pointer->next;
        }
        pointer->next = (struct words*)malloc(sizeof(struct words));
        pointer = pointer->next;
        pointer->next = NULL;
        pointer->word = (char*)malloc(sizeof(char)*(strlen(string)+1));
        strcpy(pointer->word, string);
    }
}

void createList(struct words **headPointer)
{
    FILE *fp;
    char string[80];

    if ((fp = fopen("polski.txt", "rw")) == NULL)
    {
        printf ("Nie mogê otworzyæ pliku test.txt do zapisu!\n");
        exit(-1);
    }
    else
    {
        while(fgets(string, 80, fp) != NULL)
        {
            pushBack(headPointer, string);
        }
    }
}

int seek(struct words *head, struct words **wordBeforePointer, struct words **wordAfterPointer)
{
    char string[80];

    printf("Type a word to seek:\n");
    scanf("%s", string);

    *wordBeforePointer = NULL;
    *wordAfterPointer = NULL;

    if (head != NULL)
    {
        if (strcmp(head->word, string) == 0)
        {
            return 1;
        }
        while(head->next != NULL)
        {
            head = head->next;
            printf("%s", string);
            printf("%s", head->word);
            printf("%d", strcmp(head->word, string));
            if (strcmp(head->word, string) == 0)
            {
                return 1;
            }
        }
    }
    return 0;
}

int main()
{
    struct words *head, *wordBefore, *wordAfter;
    head = NULL;
    wordBefore = NULL;
    wordAfter = NULL;


    createList(&head);
    printf("%d", seek(head, &wordBefore, &wordAfter));


    return 0;
}

最佳答案

fgets 调用实际上没有删除结尾的换行符,因此使用此方法的人经常发现 strcmp 不起作用,原因很简单:

"thisword\n" != "thisword"

如果您想手动剥离它,您可以使用类似的东西:

while (fgets (inputLine, 80, filePtr) != NULL) {
    // Get size of input line.

    size_t strSize = strlen (inputLine);

    // If there's a newline at the end, remove it.

    if ((strSize > 0) && (inputLine[strSize-1] == '\n'))
        inputLine[strSize-1] = '\0';

    // Do whatever you need to "non-newline" line.

    doSomethingWith (inputLine);
}

关于c - strcmp() 来自标准输入的字符串和来自文件的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27980760/

相关文章:

c - qsort 不排序并且输出奇怪

C:当我尝试将其中的很多东西分组时,指向指针问题的指针

c++ - 确定字符中设置位的数量

android - 字符串到字符序列

string - 如何强制 Rust 中的字符串不能为空?

php - 将所选值从下拉菜单发送到另一个页面

Python:如何使用原始列表中的某些按字母顺序排列的项目创建新列表?

c++ - 指向 Void 函数参数的指针

c - 从 C 字符串中删除多余的空格?

python - 按每个子列表中的一些值对子列表列表进行分组