In this function findBookByTitle what is supposed to happen is fp is opened and if a title matches the one given to function it will print the title. It is assumed titles are unique so once a match is found it can stop searching. My problem is i’m not entirely sure how to match the title to something in a file. This is what I have so far…
void findBookByTitle(FILE* fp, char title[])
{
FILE * fp = fopen(fp, "r");
while(!EOF && *fp = title){
printf("Title: <%c> \n", title);
}
if(EOF && *fp != title ){
printf("No books match the title: <%c> ", title);
}
}
As well when I compile I get a few errors, it might be pointless to address these as my function remains incomplete but a few of these really confuse me.
34: error: ‘fp’ redeclared as different kind of symbol
32: note: previous definition of ‘fp’ was here
34: warning: passing argument 1 of ‘fopen’ from incompatible pointer type
/usr/include/stdio.h:251: note: expected ‘const char * restrict‘ but argument is of type ‘struct FILE *’
35: error: invalid operands to binary && (have ‘int’ and ‘FILE’)
38: error: invalid operands to binary != (have ‘FILE’ and ‘char *’)
Your function is declared as taking a
FILE *as its first argument, but it then proceeds to treat that argument as if it’s a filename and try to open anotherFILE *(with the same name!) using it. Make up your mind on whether the argument is aFILE *or achar *, and change your code accordingly.You are trying to use
EOFto test for EOF onfp. It’s not quite that simple. Tryfeof(fp)instead.You are trying to read from
fpusing*fp = title(and*fp != title). This doesn’t make any sense at all. You need to use a function to read from the file pointer, such asfgetsorfscanf.