我有一个文本文件,其中的用户名和密码如下所示:
Username 999999
我试图提取这些行,以便将用户名放入一个数组中,并将密码(位于同一行上)放入一个单独的数组中。我是 C 新手,不知道如何做到这一点。
当我尝试验证用户身份验证时,我觉得可以使用正则表达式来完成此操作。例如用户输入用户名,将匹配文本文件中的行以查找包含此输入的行。用户输入密码,同一行将被验证以查看密码是否与用户名匹配。
有人知道我如何实现这个吗?
最佳答案
这是一个简单的例子--
block 引用>-- open the file "data" containing lines of the type
username number
-- loop over the file reading such lines one by one
-- for each line initialize the variable
name
with the username and the variableN
with the number-- print username and N and loop again until the end of the file is reached.
假设用户名少于100个字符,否则会出现段错误。另外,假设该数字小于usinged long,否则会被截断。
#include <stdio.h> main() { FILE *f = fopen("data", "r"); char name[100]; unsigned long n; while (fscanf(f, "%s %u", name, &n)!=EOF) { printf("%s -- %u\n", name, n); } fclose(f); }
关于c - 在 C 中返回文本文件中的部分行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46662810/