I am currently working on a C program where I need to check whether there is a string inside a string. The string may be mylog.txt.1 and I want to check that it contains mylog.txt and if it does do something.
In order to perform this I am using the following code
int logMaintenance(void *arg)
{
while (TRUE)
{
DIR *dir;
struct dirent *ent;
dir = opendir(directory);
if (dir != NULL)
{
while ((ent = readdir (dir)) != NULL)
{
if (strstr(ent->d_name, fileName) != NULL )
{
printf("%s\n", ent->d_name);
}
}
closedir(dir);
}
else
{
printf("Failed to read directory %i", EXIT_FAILURE);
}
SL_WU_SleepUSecs(2000);
}
return 0;
}
However, this code doesn't seem to be working. For some reason it will just print mylog.txt and not include any of the other files that end in .1 or .2 etc. I've also tried using >=0 instead of != NULL in the if statement and this just prints everything even if it doesn't include mylog.txt.
Thanks for any help you can provide.
fileNamedeclared and set?fileNamejust before the call tostrstr()byprintf()ing it out.