问题描述
我正在学习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;
}
解决方法
您传递的是单引号,请在第二个参数中使用双引号。
words = ['abc','acd','edf']
words = ['\'\'{}\'\''.format(x) for x in words]
print(words)