在
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