所以我有一个文件(即通讯录),其中包含有关以这种方式格式化的人的信息
——————————联系方式————————-
名字:Tony
姓氏:Stark
地址:马里布
电话号码:10203044032
电子邮件:tony.stark@jarvis.com
公司/工作地点:Stark Industries
——————————联系方式————————-
我有此代码:
stack install --flag asd:qwe
例如,当我输入“ Tony”时,它仅显示:
名字:Tony
我希望它显示联系人“ Tony”的全部信息
因此,如果您能帮助我,谢谢
答案 0 :(得分:1)
在
printf(“%s”, address_book_content);
pos = ftell(show_address_book);
fseek(show_address_book, 27, pos); /// 27 in just a random number, I just want it to change the position of the cursor in the file
printf(“%s”, address_book_content);
fseek 不变address_book_content
,所以您写两次相同的东西
您需要从计算出的位置读取文件以能够写入读取的内容,如果仍打印address_book_content
,请对其进行修改。或者只是阅读并打印您阅读的内容之后的下一行:
while ( (fgets(address_book_content, MAX_VALUE_FOR_ARRAYS, show_address_book) != NULL)
{
if ( (string_exists = strstr(address_book_content, contact_name) != NULL)
{
fputs(address_book_content, stdout); /* fputs rather printf to not write again a \n */
/* supposing it was the firstname there are 5 lines after to read and print */
fputs(fgets(address_book_content, MAX_VALUE_FOR_ARRAYS, show_address_book), stdout);
fputs(fgets(address_book_content, MAX_VALUE_FOR_ARRAYS, show_address_book), stdout);
fputs(fgets(address_book_content, MAX_VALUE_FOR_ARRAYS, show_address_book), stdout);
fputs(fgets(address_book_content, MAX_VALUE_FOR_ARRAYS, show_address_book), stdout);
fputs(fgets(address_book_content, MAX_VALUE_FOR_ARRAYS, show_address_book), stdout);
break;
}
}
警告 strstr 是不正确的方法,因为它将捕获包含所需名称的名称,例如名称 IamNotTonyAtAll 匹配 Tony >使用 strstr
当然,每次文件搜索名称时读取该信息都非常昂贵...
完整建议:
#include <stdio.h>
#include <string.h>
#define MAX_VALUE_FOR_ARRAYS 1000
int main()
{
FILE * fp = fopen("addressBook.txt", "r");
if (fp == NULL)
puts("cannot read addressBook.txt");
else {
/* use static vars to not take place into the stack */
static char contact_name[MAX_VALUE_FOR_ARRAYS];
printf("Enter the name of the contact you want to search : ");
if (scanf("%s", contact_name) != 1)
puts("invalid input");
else {
static char line1[MAX_VALUE_FOR_ARRAYS];
static char line2[MAX_VALUE_FOR_ARRAYS];
/* to have \n at the end like the read lines, it is also possible
to read the name with fgets but in case of extra spaces at the end it is not found */
strcat(contact_name, "\n");
while ((fgets(line1, MAX_VALUE_FOR_ARRAYS, fp) != NULL) &&
(fgets(line2, MAX_VALUE_FOR_ARRAYS, fp) != NULL))
{
if ((strcmp(line1 + /*bypass "First Name : "*/ 13, contact_name) == 0) || /* first name */
(strcmp(line2 + /*bypass "Last Name : "*/ 12, contact_name) == 0)) { /* last name */
/* that one */
fputs(line1, stdout);
fputs(line2, stdout);
if (fgets(line1, MAX_VALUE_FOR_ARRAYS, fp) != NULL) {
fputs(line1, stdout);
if (fgets(line1, MAX_VALUE_FOR_ARRAYS, fp) != NULL) {
fputs(line1, stdout);
if (fgets(line1, MAX_VALUE_FOR_ARRAYS, fp) != NULL) {
fputs(line1, stdout);
if (fgets(line1, MAX_VALUE_FOR_ARRAYS, fp) != NULL) {
fputs(line1, stdout);
}
}
}
}
break;
}
else {
fgets(line1, MAX_VALUE_FOR_ARRAYS, fp);
fgets(line1, MAX_VALUE_FOR_ARRAYS, fp);
fgets(line1, MAX_VALUE_FOR_ARRAYS, fp);
fgets(line1, MAX_VALUE_FOR_ARRAYS, fp);
}
}
fclose(fp);
}
}
return 0;
}
示例:
pi@raspberrypi:/tmp $ ./a.out
Enter the name of the contact you want to search : Tony
First Name : Tony
Last Name : Stark
Address : Malibu
Phone number : 10203044032
E-mail : tony.stark@jarvis.com
Company / Place of work : Stark Industries
pi@raspberrypi:/tmp $ ./a.out
Enter the name of the contact you want to search : Stark
First Name : Tony
Last Name : Stark
Address : Malibu
Phone number : 10203044032
E-mail : tony.stark@jarvis.com
Company / Place of work : Stark Industries
pi@raspberrypi:/tmp $ ./a.out
Enter the name of the contact you want to search : Bruno
First Name : Bruno
Last Name : Pages
Address : somewhere in France
Phone number : 123456789
E-mail : me@hiddenAddress.fr
Company / Place of work : BoUML unlimited
假设 addressBook.txt 包含:
First Name : Tony
Last Name : Stark
Address : Malibu
Phone number : 10203044032
E-mail : tony.stark@jarvis.com
Company / Place of work : Stark Industries
First Name : Bruno
Last Name : Pages
Address : somewhere in France
Phone number : 123456789
E-mail : me@hiddenAddress.fr
Company / Place of work : BoUML unlimited