This d_name is part of a const struct, so it is not guaranteed to be
modifiable. By converting it to a pointer to non-const char you're declaring
your intent to modify it. That's the warning.
>60: char *snippet;
>61: char *treffer = strtok(buff, ".");

or just say it refers to the initialisation of 'buff'.
> I'd like to make shure scandir targets only '*.xdb' files. I first took
>
> if ( strstr( namelist->d_name, ".xdb" ) != NULL )
> return (1);
> but that targets '*.xdb*' so I cannot use it.

Yes, that's right. The looping you do will work but strtok has all
sorts of issues that make it a bad idea. For one thing, altering the
string makes the result very odd (try printing the name when you've
matched x.y.xdb). You don't need to alter the name at all. strrchr
will find the last occurrence of a character (or return NULL) so you can
test for

Note that I prefer to return the result of a test rather than test it in
an if statement and then return either 0 or 1. That's just too much
code.
> My code above works without errors, but I don't know why
> - strcmp(namelist->d_name, "." ) operates fine without warning and
> char *buff=namelist->d_name; does not.

You declared namelist to be a pointer to a const structure. That names
all it members const as well. Had you written

const char *buff = namelist->d_name;

you'd be OK, but then you'd get an error trying to pass buff to strtok.
One way or another, having declared namelist to point to a const object
you can get a non-const pointer to any part of it without a warning or
an error from the compiler.

strcmp's first argument is declared as a void pointer to const, so there
is no complaint about that version.
> Would you please enlighten me and what if you can tell how to make it
> without warnings, thanks.

A small point, do you need to test for "." and ".." first? I would have
though that's covered by you other test. Also, return is clearer
without the ()s. Have you been looking at very old C? The ()s in
return (1); used to be required a very, very long time ago!

Thanks for guiding my next posting.
>
> or just say it refers to the initialisation of 'buff'.
>
>> I'd like to make shure scandir targets only '*.xdb' files. I first took
>>
>> if ( strstr( namelist->d_name, ".xdb" ) != NULL )
>> return (1);
>> but that targets '*.xdb*' so I cannot use it.
>
> Yes, that's right. The looping you do will work but strtok has all
> sorts of issues that make it a bad idea. For one thing, altering the
> string makes the result very odd (try printing the name when you've
> matched x.y.xdb). You don't need to alter the name at all. strrchr
> will find the last occurrence of a character (or return NULL) so you can
> test for

And thank you both for pointing me to strrchr which turns using strcmp
into success at this point. Great.
> Note that I prefer to return the result of a test rather than test it in
> an if statement and then return either 0 or 1. That's just too much
> code.
>
>> My code above works without errors, but I don't know why
>> - strcmp(namelist->d_name, "." ) operates fine without warning and
>> char *buff=namelist->d_name; does not.
>
> You declared namelist to be a pointer to a const structure. That names
> all it members const as well. Had you written
>
> const char *buff = namelist->d_name;
>
> you'd be OK, but then you'd get an error trying to pass buff to strtok.

Well, yes. You hit me.
> One way or another, having declared namelist to point to a const object
> you can get a non-const pointer to any part of it without a warning or
> an error from the compiler.
>
> strcmp's first argument is declared as a void pointer to const, so there
> is no complaint about that version.

I learned that now, thanks to you and Alan.
>
>> Would you please enlighten me and what if you can tell how to make it
>> without warnings, thanks.
>
> A small point, do you need to test for "." and ".." first? I would have
> though that's covered by you other test. Also, return is clearer
> without the ()s. Have you been looking at very old C? The ()s in
> return (1); used to be required a very, very long time ago!
>

Right.

I am learning by doing and where I am not smart enough I google for code
which works well so far.

I have one C Programming book from 2004, that is from Suse Press "C
Programmierung unter Linux, Window, Unix" I assume it is a good starting
point but lacks professional coding style.

At least I have understood dynamic array management I needed to know for
my little project which makes it a good book

I am working at a program which reads mounted devices at /media and
interactively reads and writes to sqlite3 databases about the media
contents. See, I spend my spare time in writing C, lately, I haven't
studied programming. It's fun to make your tools yourself.

As I haven't found a lot or a lot different examples for filtering
scandir, I was lost, so I had to ask here. I was sure you guys would
read and I am happy about your competent answers. Finding the database
files works, now.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!