微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法使用strtok

如何解决无法使用strtok

我正在学习C,我需要构建代码以读取和比较CSV文件中的用户登录信息。我正在尝试使用strtok()函数在数组中分离CSV值。但是,当我调试代码(使用Visual Studio Code)时,它在Cannot access memory at address显示错误user_data_column = strtok(line,',');。我没有找到适合特定情况的搜索结果,但可以成功。完整的代码

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include <locale.h>
    
char username[50],password[50];
char cwd[PATH_MAX];
void show_login()
{
    typedef struct
    {
        char *user;
        char *password;
    } * user;

    char *user_data_column;
    user *user_data;

    getcwd(cwd,sizeof(cwd));
    strcat(cwd,"/data/Users.csv");

    FILE *users_list = fopen(cwd,"r");
    long file_length = ftell(users_list);
    char *buffer = malloc(file_length);
    char line[PATH_MAX];
    int line_counter = 0;
    int is_authorized = 0;

    printf("\bLogin\n");
    printf("Nome do usuário: ");
    fgets(username,sizeof(username),stdin);
    printf("Senha: ");
    fgets(password,sizeof(password),stdin);

    if (users_list)
    {
        if (buffer)
        {
            while (fgets(line,sizeof(line),users_list) != NULL)
            {
                if (line_counter == 0)
                {
                    line_counter++;

                    continue;
                }
                user_data_column = strtok(line,');
            }
        }
    }
    else
    {
        printf("Erro! O arquivo Users.csv não foi enconTrado\n");
        exit(1);
    }
    fclose(users_list);
}

int main()
{
    show_login();
    return 0;
}

(未完成比较登录的步骤) 错误

enter image description here

解决方法

您传递的是单引号,请在第二个参数中使用双引号。

words = ['abc','acd','edf']
words = ['\'\'{}\'\''.format(x) for x in words]
print(words)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。