0

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.

7
  • ;) example usage Commented May 3, 2012 at 12:22
  • How is fileName declared and set? Commented May 3, 2012 at 12:43
  • Its a global variable char fileName[FILE_PATH_BUF_LEN]; and is set by fileName = "mylog.txt"; Commented May 3, 2012 at 12:46
  • I'd propose that for debugging you log fileNamejust before the call to strstr() by printf()ing it out. Commented May 3, 2012 at 12:50
  • I have tried that and filename is the value that I am expecting Commented May 3, 2012 at 12:52

2 Answers 2

1

ANSI/ISO C provides the char *strstr(const char *haystack, const char *needle) function, allowing to find a needle in a haystack. Use #include <string.h> to get the prototype. Are you sure you have the args in the proper order?

Edit: I had the haystack and needle in the wrong order, blush.

Sign up to request clarification or add additional context in comments.

5 Comments

Its the other way round. Needle should be the second parameter and haystack the first. strstr
Are you sure you have the args in the proper order? ;-)
I have tried them both ways round when I try it the other way I get mylog.txt and on the next line a .
@Boardy I was referring to Jens' answer ... ;-)
Uh-oh, I goofed big time, yes, I had the parameters in the wrong order. Thanks for pointing out this mistake!
0

You might have the parameters to strstr() the wrong way around. It's hard to tell from your description. Are you looking for fileName in ent->d_name, or the other way around?

The first is the string to search inside. The second is the string to search for.

Otherwise, try creating a test case with fixed data.

2 Comments

This file name is set at the start of the program and won't change during execution, ent->d_name is the name of the files/directories within a directory that I am looping through. ent->d_name should include the string filename
I've also tried reverting the parameters but then I get mylog.txt and then a . on the next line as if its two separate files its found

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.