让我们从一种非常基本的方法开始,该方法使用平面文件进行存储,并且在每个查询中,循环遍历平面文件以尝试将您key
与第一个字段匹配。如果找到匹配项,只需使用分隔值(使用赋值抑制修饰符sscanf
忽略第一个字段到该字段的转换说明符,例如)。打印值。如果在文件中找不到密钥,也请这样说。sscanf
'*'
使用fgets()
for input,您可以简单地进行输入,直到用户按下Enter该键的空白行。(您只需检查第一个字符是否是'\n'
,如果是,则中断循环)。
您不断循环以允许进行多个键查询,rewind()
每次循环开始时都使用回退到文件的开头。
将它们放在一起,您可以首先打开并验证您的文件是否已打开以供阅读。
#include <stdio.h>
#include <string.h>
#define MAXN 64 /* if you need a constand, #define one (or more) */
#define MAXC 1024 /* (don't skimp on buffer size) */
int main (int argc, char **argv) {
/* use filename provided as 1st argument ("flatfile.txt" by default) */
FILE *fp = fopen (argc > 1 ? argv[1] : "flatfile.txt", "r");
if (!fp) { /* validate file open for reading */
perror ("file open failed");
return 1;
}
现在开始你的持续循环,它将key
从用户那里读取,从键的末尾修剪'\n'
(你不希望它作为比较的一部分),并获取键的长度:
for (;;) { /* loop continually until [Enter] on empty line */
char buf[MAXC], name[MAXN], city[MAXN], key[MAXN];
unsigned salary, len, found = 0;
rewind (fp); /* rewind file to beginning */
fputs ("\nInput the primary key: ", stdout); /* prompt for key */
if (!fgets (key, MAXN, stdin) || *key == '\n') /* read key */
break;
key[strcspn (key, "\n")] = 0; /* trim '\n' */
len = strlen(key); /* get key length */
有了这些信息,现在循环遍历文件,将每一行读入缓冲区buf
,然后strcmp
将第一个len
字符与您key
的found
. 最后如果found
没有设置,让用户知道没有找到密钥,现在再做一次,直到用户Enter单独按下要求主键的行:
while (fgets (buf, MAXC, fp)) { /* read each line */
if (strncmp (buf, key, len) == 0) { /* compare key */
/* parse line into separate values, ignoring 1st key field */
if (sscanf (buf, "%*63[^|]|%63[^|]|%63[^|]|%u",
name, city, &salary) == 3) {
printf ("\nResult:\nName = %s\nAddress = %s\n"
"Salary = %u\n", name, city, salary);
found = 1; /* set flag indicating key found */
break; /* no sense in reading rest */
}
}
}
if (!found) /* if key not found, so indicate */
fputs ("\nResult: (not found)\n", stdout);
}
剩下的就是关闭输入文件。一个完整的例子是:
#include <stdio.h>
#include <string.h>
#define MAXN 64 /* if you need a constand, #define one (or more) */
#define MAXC 1024 /* (don't skimp on buffer size) */
int main (int argc, char **argv) {
/* use filename provided as 1st argument ("flatfile.txt" by default) */
FILE *fp = fopen (argc > 1 ? argv[1] : "flatfile.txt", "r");
if (!fp) { /* validate file open for reading */
perror ("file open failed");
return 1;
}
for (;;) { /* loop continually until [Enter] on empty line */
char buf[MAXC], name[MAXN], city[MAXN], key[MAXN];
unsigned salary, len, found = 0;
rewind (fp); /* rewind file to beginning */
fputs ("\nInput the primary key: ", stdout); /* prompt for key */
if (!fgets (key, MAXN, stdin) || *key == '\n') /* read key */
break;
key[strcspn (key, "\n")] = 0; /* trim '\n' */
len = strlen(key); /* get key length */
while (fgets (buf, MAXC, fp)) { /* read each line */
if (strncmp (buf, key, len) == 0) { /* compare key */
/* parse line into separate values, ignoring 1st key field */
if (sscanf (buf, "%*63[^|]|%63[^|]|%63[^|]|%u",
name, city, &salary) == 3) {
printf ("\nResult:\nName = %s\nAddress = %s\n"
"Salary = %u\n", name, city, salary);
found = 1; /* set flag indicating key found */
break; /* no sense in reading rest */
}
}
}
if (!found) /* if key not found, so indicate */
fputs ("\nResult: (not found)\n", stdout);
}
fclose (fp); /* close file */
}
示例输入文件
$ cat dat/flatfile.txt
A0001|John|New York City|12000
A0002|Daisy|New Delhi|32000
A0003|Dany|London|23000
示例使用/输出
$ ./bin/readflatfile dat/flatfile.txt
Input the primary key: A0002
Result:
Name = Daisy
Address = New Delhi
Salary = 32000
Input the primary key: A0003
Result:
Name = Dany
Address = London
Salary = 23000
Input the primary key: A0004
Result: (not found)
Input the primary key:
如果您还有其他问题,请仔细查看并告诉我。