我正在与 c 中的 strcmp 作斗争

发布于 2024-11-04 14:03:27 字数 616 浏览 0 评论 0原文

我正在努力将用户输入与文件内容进行比较。基本上,我创建了一个更新员工详细信息的函数,该函数首先提示输入员工的姓名,然后将输入的姓名与文件中的姓名进行比较。我尝试这样做:

void update(EMPLOYEE *details)
{
    FILE *file;
    char search;
    file = fopen("employees.txt","r");
    if(file == NULL)
    {
        printf("File error!!!");
        exit(0);
    }
    else
    {
        search = getc(file);
        printf("Enter name: ");
        scanf("%s",details->name);

        if((strcmp(search,details->name) == 0))
        {
            printf("%s\n",details->name);
            printf("%d",details->employeeNumber);
        }
    }

    fclose(file);
}

i'm struggling to compare user input with the file contents. Basically i created a function that updates employee details that first prompts for the name of the employee and then it compares the name entered with those in the file. I tried doing this:

void update(EMPLOYEE *details)
{
    FILE *file;
    char search;
    file = fopen("employees.txt","r");
    if(file == NULL)
    {
        printf("File error!!!");
        exit(0);
    }
    else
    {
        search = getc(file);
        printf("Enter name: ");
        scanf("%s",details->name);

        if((strcmp(search,details->name) == 0))
        {
            printf("%s\n",details->name);
            printf("%d",details->employeeNumber);
        }
    }

    fclose(file);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撩人痒 2024-11-11 14:03:27

您正在将单个字符 search 与(我假设)整个字符串 details->name 进行比较。

strcmp 的方法签名是

int strcmp (const char *str1, const char *str2);

:调用它:

int strcmp (const char str1, const char *str2);

不仅如此,如果您修复它以使其编译,您将读取您的 str1 字符缓冲区,除非它碰巧是字符 0,但事实并非如此。

要解决此问题,您必须执行以下操作之一:

  • 将比较更改为仅查看第一个字符。
  • 将搜索变量更改为字符串(可能是 char[])并读取整个字符串,然后进行比较。

You are comparing a single character, search with (I assume) an entire string, details->name.

The method signature for strcmp is:

int strcmp (const char *str1, const char *str2);

You are calling it as:

int strcmp (const char str1, const char *str2);

Not only this, but you are going to read past your str1 character buffer if you fix it to make it compile unless it happens to be character 0, which it won't be.

To fix this, you must do one of the following:

  • Change your comparison to look at just the first characters.
  • Change your search variable to be a string (probably char[]) and read in an entire string, then compare.
跨年 2024-11-11 14:03:27

strcmp 采用字符串 (char *),而不是单个字母。正如其他人在评论中提到的,您需要更新 getc 进程以读取多个字母。

strcmp takes a string (char *) not a single letter. As others have mentioned in the comments, you need to update the getc process to read more than the single letter.

故事和酒 2024-11-11 14:03:27

提示:

EMPLOYEE fileData;

while ( fread( &fileData, sizeof( fileData ), 1, file ) == sizeof( fileData )
{
...
}

编辑:将 read 更改为 fread,因为它是 FILE*

hth

a hint:

EMPLOYEE fileData;

while ( fread( &fileData, sizeof( fileData ), 1, file ) == sizeof( fileData )
{
...
}

EDIT: changed read to fread since it is a FILE*

hth

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文