我有一个平面文件(txt 文件),其中包含一些字段:
主键、姓名、地址、薪水 - 用 | 分隔 喜欢:
A0001|John|New York City|12000
A0002|Daisy|New Delhi|32000
A0003|Dany|London|23000
.. 等等
当我有主键时,如何开发代码来搜索平面文件中的每个数据? 并将它的每个数据存储到特定的变量中以供下一个用途。
我想要的结果是:
Input the primary key: A0002
Result:
Name = Daisy
Address = New Delhi
Salary = 32000
Input the primary key:
最佳答案
让我们从一个非常基本的方法开始,该方法使用平面文件进行存储,并在每个查询中循环平面文件,尝试将您的 key
与第一个字段相匹配。如果找到匹配项,只需使用 sscanf
来分隔值(忽略第一个字段,使用 assignment suppression modifier 到您的 sscanf
转换说明符该字段,例如 '*'
)。打印值。如果在文件中找不到 key ,也请说明。
使用 fgets()
进行输入,您可以简单地进行输入,直到用户在空白行上按下 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
,从 key 的末尾修剪 '\n'
(你不想要那个作为比较的一部分),并获取 key 的长度:
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
没有设置,让用户知道 key 没有找到,现在再做一次,直到用户在请求的行上单独按下 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:
检查一下,如果您还有其他问题,请告诉我。
关于c - 如何读取平面文件并将数据放入相应的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58479976/