Exercise for exam about comparing string on file
Posted
by
Telmo Vaz
on Stack Overflow
See other posts from Stack Overflow
or by Telmo Vaz
Published on 2013-06-27T16:18:53Z
Indexed on
2013/06/27
16:21 UTC
Read the original article
Hit count: 243
I'm trying to do this exercise for my exam tomorrow.
I need to compare a string of my own input and see if that string is appearing on the file. This needs to be done directly on the file, so I cannot extract the string to my program and compare them "indirectly".
I found this way but I'm not getting it right, and I don't know why. The algorithm sounds good to me.
Any help, please? I really need to focus on this one.
Thanks in advance, guys.
#include<stdio.h>
void comp();
int main(void)
{
comp();
return 0;
}
void comp()
{
FILE *file = fopen("e1.txt", "r+");
if(!file)
{
printf("Not possible to open the file");
return;
}
char src[50], ch;
short i, len;
fprintf(stdout, "What are you looking for? \nwrite: ");
fgets(src, 200, stdin);
len = strlen(src);
while((ch = fgetc(file)) != EOF)
{
i = 0;
while(ch == src[i])
{
if(i <= len)
{
printf("%c - %c", ch, src[i]);
fseek(file, 0, SEEK_CUR + 1);
i++;
}
else break;
}
}
}
© Stack Overflow or respective owner