问题
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);
}
}
}
The text files say:
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....
回答1:
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);
}
....
EDIT:
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);
Should do what you need (with the 200 to prevent buffer overflows).
回答2:
Your code would work if you only matched for Making The Right Changes, because your fscanf line is doing something you didn't expect.
You do this to read each line of your file into separate fields.
fscanf(fp, "%[^;];%[^;];%[^\n]", title2, last, first);
The [^\n] at the end tells fscanf to ignore the newline, but it remains in the buffer, so it will actually read it in for the next line it reads in. This means that every book's title has a \n character prepended to the beginning.
I changed it to this:
fscanf(fp, "%[^;];%[^;];%s\n", title2, last, first);
Which means it will just read the line and break it up as you expect (and drop the newline on the floor).
Here's a sample program I wrote based on yours, with a simple main function.
#include <stdio.h>
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, "%[^;];%[^;];%s\n", title2, last, first);
if( strcmp(title2,title)==0)
{
printf("I found a match\n");
printf("Title: %s\n", title2);
printf("Author: %s,%s\n", last,first);
printf("---\n");
return;
}
}
}
int main(int argc, char **argv) {
char *fname = argv[1];
FILE* fp = fopen(fname, "r");
findBookByTitle(fp, "Making The Right Choices");
findBookByTitle(fp, "Time For Change");
findBookByTitle(fp, "The Right Moves");
fclose(fp);
return 0;
}
Running this code, I get correct output:
λ > ./a.out sample.txt
I found a match
Title: Making The Right Choices
Author: Henry,Mark
---
I found a match
Title: Time For Change
Author: Robinson,Chris
---
I found a match
Title: The Right Moves
Author: Henry,Mark
---
来源:https://stackoverflow.com/questions/10590114/c-programming-search-file-for-string