字符串C编程搜索文件 [英] c programming search file for string

查看:100
本文介绍了字符串C编程搜索文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我必须写一个函数,从文件中读取并对其进行扫描,看看是否有其中的标题,用户放入并打印所有现有的标题格式为标题标题匹配:(标题)作者:(姓,名)。如果没有标题匹配,那么它打印未发现标题。我可以得到程序的标题读入一个数组,并打印了我想要的格式,但我的问题是搜索文件,以查找匹配的标题将它们打印出来。该程序只是打印了没有标题匹配,甚至5倍当有match..any帮助将不胜AP preciated ...谢谢...

so I have to write a function that reads from a file and scans it to see if any of the titles within it matches the title that the user puts in and prints all of the existing titles in the format title: (title) Author:(last name, first name). if no titles match then it prints no titles found. I can get the program to read in the titles into an array and also print in the format that i want, but my problem is searching the file to find matching titles to print them.. The program just prints off no titles match 5 times even when there is a match..any help would be greatly appreciated...Thank you...

void findBookByTitle(FILE* fp, char title[])
{
FILE* open = fp;
char title2[200];
char last[200];
char first[200];
int i=0;

while(!feof(fp))
{
fscanf(fp, "%[^;];%[^;];%[^\n]", title2, last, first);
if( strcmp(title2,title)==0)
{
printf("Title: %s\n", title2);
printf("Author: %s,%s\n", last,first);
}
else {
   printf("No books match the title: %s\n", title);
 }

}
}

该文本文件说:

Making The Right Choices; Henry; Mark
Time For Change; Robinson; Chris
Battle For Air; Jetson; Lola
The Right Moves; Henry;Mark
People Today; Robinson; Chris                                             

因此​​,如果用户想要搜索的书的时间去改变它会打印出作者:时间变化
作者:亨利,马克
但我的功能只是打印了无书遍地匹配....

So if the user wants to search for book Time to change it would print out author: time for change Author: Henry, Mark But my function just prints off no books match over and over....

推荐答案

问题是你的其他子句的位置。

The problem is the location of your else clause.

如果您修复的间距上这样的:

If you fix the spacing on this:

while(!feof(fp)) {
    fscanf(fp, "%[^;];%[^;];%[^\n]", title2, last, first);
    if( strcmp(title2,title)==0) {
        printf("Title: %s\n", title2);
        printf("Author: %s,%s\n", last,first);
    }
    else {
        printf("No books match the title: %s\n", title);
    }
}

您可以看到,如果发现不匹配的每个标题你做的:

You can see that for EACH title found if it doesn't match you do:

    else {
        printf("No books match the title: %s\n", title);
    }

您需要做的就是添加一个变量来看看你是否已经找到任何东西,检查一下你读到的一切后,

What you need to do is add a variable to see if you have found anything and check it after you read everything

int found = 0;
...
while(!feof(fp)) {
    fscanf(fp, "%[^;];%[^;];%[^\n]", title2, last, first);
    if( strcmp(title2,title)==0) {
        printf("Title: %s\n", title2);
        printf("Author: %s,%s\n", last,first);
        found = 1;
    }
}

if(!found) {
   printf("No books match the title: %s\n", title);
}
....

编辑:

从这又问题向您展示了如何使用<$省略字符C $ C>的fscanf 。基于答案了,我想:

From another question This shows you how to omit characters using fscanf. Based on the answer there I think:

fscanf(fp, "%200[^;]%*c %200[^;]%*C %200[^\n]%*c", title2, last, first);

如果你所需要的(与200至prevent缓冲区溢出)。

Should do what you need (with the 200 to prevent buffer overflows).

这篇关于字符串C编程搜索文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆