所以我有一个文件(那是一个地址簿),里面有关于人们的信息,格式是这样的
---------接触---------
名字:托尼
姓氏:斯塔克
地址:马里布
电话:10203044032
电子邮箱:[email protected]
公司/工作地点:斯塔克工业公司
---------接触---------
我有这个密码:
#define MAX_VALUE_FOR_ARRAYS 1000
int main()
{
long pos = 0; /// this will store the position of the cursor
char address_book_content[MAX_VALUE_FOR_ARRAYS];
char contact_name[MAX_VALUE_FOR_ARRAYS];
char *string_exists = NULL;
File *show_address_book = NULL;
show_address_book = fopen("addressBook.txt", "r");
printf("Enter the name of the contact you want to search");
scanf("%s", &contact_name);
/** I want : when the user inputs a name, the program searches it in the file and if it’s found it prints the rest of the file starting from the line where that name is. So I tried the following **/
while ( (fgets(address_book_content, MAX_VALUE_FOR_ARRAYS, show_address_book) != NULL)
{
if ( (string_exists = strstr(address_book_content, contact_name) != NULL)
{
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);
}
}
return 0;
}
例如,当我输入“Tony”时,它只显示:
名字:托尼
我要它显示联系人“托尼”的全部信息
如果你能帮我,谢谢
最佳答案
在里面
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
是否仍在打印。或者只需阅读并打印阅读后的下一行: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使用strstr匹配Tony
当然这是相当昂贵的读取每次文件搜索一个名字。。。
完整的建议:
#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 : [email protected]
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 : [email protected]
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 : [email protected]
Company / Place of work : BoUML unlimited
假设addressBook.txt包含:
First Name : Tony
Last Name : Stark
Address : Malibu
Phone number : 10203044032
E-mail : [email protected]
Company / Place of work : Stark Industries
First Name : Bruno
Last Name : Pages
Address : somewhere in France
Phone number : 123456789
E-mail : [email protected]
Company / Place of work : BoUML unlimited