Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #99098 > unrolled thread

C newb - Part III - find a .jhr file in a directory - seg fault

Started byS390 <s390@tampascanner.info>
First post2016-12-12 17:58 +0000
Last post2016-12-23 22:35 +0000
Articles 12 — 7 participants

Back to article view | Back to comp.lang.c


Contents

  C newb - Part III - find a .jhr file in a directory - seg fault S390 <s390@tampascanner.info> - 2016-12-12 17:58 +0000
    Re: C newb - Part III - find a .jhr file in a directory - seg fault Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-12 22:18 +0000
      Re: C newb - Part III - find a .jhr file in a directory - seg fault S390 <s390@tampascanner.info> - 2016-12-20 22:12 +0000
        Re: C newb - Part III - find a .jhr file in a directory - seg fault Barry Schwarz <schwarzb@dqel.com> - 2016-12-20 14:38 -0800
          Re: C newb - Part III - find a .jhr file in a directory - seg fault S390 <s390@tampascanner.info> - 2016-12-23 16:08 +0000
            Re: C newb - Part III - find a .jhr file in a directory - seg fault Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-23 16:34 +0000
            Re: C newb - Part III - find a .jhr file in a directory - seg fault "James R. Kuyper" <jameskuyper@verizon.net> - 2017-01-03 16:16 -0500
    Re: C newb - Part III - find a .jhr file in a directory - seg fault Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2016-12-23 12:22 -0500
    Re: C newb - Part III - find a .jhr file in a directory - seg fault BartC <bc@freeuk.com> - 2016-12-23 17:40 +0000
      Re: C newb - Part III - find a .jhr file in a directory - seg fault Keith Thompson <kst-u@mib.org> - 2016-12-23 14:13 -0800
        Re: C newb - Part III - find a .jhr file in a directory - seg fault Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2016-12-23 17:22 -0500
        Re: C newb - Part III - find a .jhr file in a directory - seg fault BartC <bc@freeuk.com> - 2016-12-23 22:35 +0000

#99098 — C newb - Part III - find a .jhr file in a directory - seg fault

FromS390 <s390@tampascanner.info>
Date2016-12-12 17:58 +0000
SubjectC newb - Part III - find a .jhr file in a directory - seg fault
Message-ID<azB3A.89805$dL2.72348@fx12.iad>
In looking to build up the blocks of what I want to do for my program, I 
am starting with one piece of that.

finding all *.jhr in a directory

So looking over some examples....and building from them I the below, but 
when doing the test to find *.jhr files it seg faults... tried on a 
couple versions of GCC

/jamtest $ gcc --version
gcc (Ubuntu/Linaro 4.4.4-14ubuntu5.1) 4.4.5

@fidodev:~/jamtest$ gcc --version
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.

I am lost at this point... why the test for the *.jhr file fails and seg 
faults...


$ cat dirtest.c 

#include <dirent.h> 
#include <stdio.h> 
#include <string.h>

int main(void)
{
  char hdrfile;
  int len;
  const char ch = '.';
  char *ret;
  DIR           *d;
  struct dirent *dir;
  d = opendir(".");
  if (d)
  {
    while ((dir = readdir(d)) != NULL)
    {
     ret = strchr(dir->d_name, ch);
     printf("String after |%c| is - |%s|\n", ch, ret);    
     
      //if (strcmp(dir->d_name,"TESTJAMNNTPD.jhr") == 0) //
     if (strcmp(ret,".jhr") != 0)
        printf("%s\n", dir->d_name);
    }

    closedir(d);
  }

  return(0);

This results in:

@fidodev:~/jamtest$ make dirtest
cc     dirtest.c   -o dirtest
@fidodev:~/jamtest$ ./dirtest 
String after |.| is - |..|
..
String after |.| is - |.jlr|
TESTJAMNNTPD.jlr
String after |.| is - |.jdx|
TESTJAMNNTPD.jdx
String after |.| is - |(null)|
Segmentation fault (core dumped)

BUT

if I remove the test to find the the .jhr file(s).

$ cat dirtest.c 

#include <dirent.h> 
#include <stdio.h> 
#include <string.h>

int main(void)
{
  char hdrfile;
  int len;
  const char ch = '.';
  char *ret;
  DIR           *d;
  struct dirent *dir;
  d = opendir(".");
  if (d)
  {
    while ((dir = readdir(d)) != NULL)
    {
     ret = strchr(dir->d_name, ch);
     printf("String after |%c| is - |%s|\n", ch, ret);    
     
     if (strcmp(dir->d_name,"TESTJAMNNTPD.jhr") == 0) 
     // if (strcmp(ret,".jhr") != 0)//
        printf("%s\n", dir->d_name);
    }

    closedir(d);
  }

  return(0);

This results in:

@fidodev:~/jamtest$ make dirtest
cc     dirtest.c   -o dirtest
@fidodev:~/jamtest$ ./dirtest 
String after |.| is - |..|
..
String after |.| is - |.jlr|
TESTJAMNNTPD.jlr
String after |.| is - |.jdx|
TESTJAMNNTPD.jdx
String after |.| is - |(null)|
Segmentation fault (core dumped)

BUT

if I remove the test to find the the .jhr file(s).


$ cat dirtest.c 

#include <dirent.h> 
#include <stdio.h> 
#include <string.h>

int main(void)
{
  char hdrfile;
  int len;
  const char ch = '.';
  char *ret;
  DIR           *d;
  struct dirent *dir;
  d = opendir(".");
  if (d)
  {
    while ((dir = readdir(d)) != NULL)
    {
     ret = strchr(dir->d_name, ch);
     printf("String after |%c| is - |%s|\n", ch, ret);    
     
     //if (strcmp(dir->d_name,"TESTJAMNNTPD.jhr") == 0)  //
     if (strcmp(ret,".jhr") != 0)
        printf("%s\n", dir->d_name);
    }

    closedir(d);
  }

  return(0);

$ make dirtest
cc     dirtest.c   -o dirtest
@fidodev:~/jamtest$ ./dirtest
String after |.| is - |..|
String after |.| is - |.jlr|
String after |.| is - |.jdx|
String after |.| is - |(null)|
String after |.| is - |.out|
String after |.| is - |.jdt|
String after |.| is - |.|
String after |.| is - |.jhr|
TESTJAMNNTPD.jhr
String after |.| is - |(null)|
String after |.| is - |.cmhw|
String after |.| is - |(null)|
String after |.| is - |.c|
String after |.| is - |.c|
@fidodev:~/jamtest$ 

Does not matter if the test is to find NOT .jhr or to find .jhr

this is if (strcmp(ret,".jhr") == 0)

$ ./dirtest
String after |.| is - |..|
String after |.| is - |.jlr|
String after |.| is - |.jdx|
String after |.| is - |(null)|
Segmentation fault (core dumped)


directory:
~/jamtest$ ls -la
total 88
drwxrwxr-x  3 me me  4096 Dec 12 12:40 .
drwxr-xr-x 23 me me  4096 Dec 12 12:34 ..
-rwxrwxr-x  1 me me 17148 Dec  2 14:35 a.out
-rwxrwxr-x  1 me me  8837 Dec 12 12:40 dirtest
-rw-r--r--  1 me me   613 Dec 12 12:40 dirtest.c
drwxr-xr-x  2 me me  4096 Dec  2 14:33 jamlib
-rwxrwxr-x  1 me me 17184 Dec  6 21:58 jamtest
-rw-r--r--  1 me me   546 Dec  6 21:58 jamtest.c
-rw-r--r--  1 me me     4 Dec  2 14:33 TESTJAMNNTPD.cmhw
-rw-r--r--  1 me me   268 Dec  2 14:33 TESTJAMNNTPD.jdt
-rw-r--r--  1 me me    24 Dec  2 14:33 TESTJAMNNTPD.jdx
-rw-r--r--  1 me me  1839 Dec  2 14:33 TESTJAMNNTPD.jhr
-rw-r--r--  1 me me     0 Dec  2 14:33 TESTJAMNNTPD.jlr


-- 
I MADE America Great Again! President Trump! I put a stake in that bitch 
for good!

[toc] | [next] | [standalone]


#99142

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2016-12-12 22:18 +0000
Message-ID<87k2b4pzac.fsf@bsb.me.uk>
In reply to#99098
S390 <s390@tampascanner.info> writes:
<snip>
> I am lost at this point... why the test for the *.jhr file fails and seg 
> faults...
>
>
> $ cat dirtest.c 
>
> #include <dirent.h> 
> #include <stdio.h> 
> #include <string.h>
>
> int main(void)
> {
>   char hdrfile;
>   int len;
>   const char ch = '.';
>   char *ret;
>   DIR           *d;
>   struct dirent *dir;
>   d = opendir(".");
>   if (d)
>   {
>     while ((dir = readdir(d)) != NULL)
>     {
>      ret = strchr(dir->d_name, ch);

When the name does not have a '.' in it, the result will be a null
pointer -- a special value that does not point at anything, often used
to indicate failure in some way.  Doing anything with a null pointer is
a Bad Thing, but printf:

>      printf("String after |%c| is - |%s|\n", ch, ret);

tries to help but printing (null), but this test

>      if (strcmp(ret,".jhr") != 0)

Will almost certainly go wrong.

>         printf("%s\n", dir->d_name);
>     }
>
>     closedir(d);
>   }
>
>   return(0);

<snip>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#99943

FromS390 <s390@tampascanner.info>
Date2016-12-20 22:12 +0000
Message-ID<11i6A.85165$Cx7.36410@fx11.iad>
In reply to#99142
On Mon, 12 Dec 2016 22:18:03 +0000, Ben Bacarisse wrote:

> When the name does not have a '.' in it, the result will be a null
> pointer -- a special value that does not point at anything, often used
> to indicate failure in some way.  Doing anything with a null pointer is
> a Bad Thing, but printf:

I don't want to go down the rabbit hole on this too much.. but this seems 
to be an C issue with these  "pointers"

I am not interested in a "pointer"

If I do something like this

If x=3.141

or

If x="STupid C Thing" 

I am not asking about "pointers" I am only interested in the value of x 
be it what ever, and if its empty/nothing aka null, then the test would 
fail... as empty/nothing/null is not either of the values I tested for 
above... Seems to be no point to this.

Why??? See below....
 

>>      if (strcmp(ret,".jhr") != 0)
> 
> Will almost certainly go wrong.
> 

This is why its bad!

So this nested if loop is the /*solution?*/

Seems terribly sloppy to me!

#include <dirent.h> 
#include <stdio.h> 
#include <string.h>

int main(void)
{
  char hdrfile;
  int len;
  const char ch = '.';
  char *ret;
  DIR           *d;
  struct dirent *dir;
  d = opendir(".");
  if (d)
  {
    while ((dir = readdir(d)) != NULL)
    {
     ret = strchr(dir->d_name, ch);
     //printf("String after |%c| is - |%s|\n", ch, ret);    
      if (ret)
      {
	if (strcmp(ret,".jhr") == 0)
	{
	printf("ret is: |%s|\n",dir->d_name);
	}
      }
      
     //end while //
     }

    closedir(d);
  }

  return(0);
}


-- 
I MADE America Great Again! President Trump! I put a stake in that bitch 
for good!

[toc] | [prev] | [next] | [standalone]


#99946

FromBarry Schwarz <schwarzb@dqel.com>
Date2016-12-20 14:38 -0800
Message-ID<66cj5cp9700hkumt1ed5nbc3i4jdlph1d0@4ax.com>
In reply to#99943
On Tue, 20 Dec 2016 22:12:13 GMT, S390 <s390@tampascanner.info> wrote:

>On Mon, 12 Dec 2016 22:18:03 +0000, Ben Bacarisse wrote:
>
>> When the name does not have a '.' in it, the result will be a null
>> pointer -- a special value that does not point at anything, often used
>> to indicate failure in some way.  Doing anything with a null pointer is
>> a Bad Thing, but printf:
>
>I don't want to go down the rabbit hole on this too much.. but this seems 
>to be an C issue with these  "pointers"
>
>I am not interested in a "pointer"

Perhaps you should be.

>If I do something like this
>
>If x=3.141
>
>or
>
>If x="STupid C Thing" 
>
>I am not asking about "pointers" I am only interested in the value of x 

Ignoring the mistake of = when you meant ==, your second test is most
certainly asking about pointers.

>be it what ever, and if its empty/nothing aka null, then the test would 
>fail... as empty/nothing/null is not either of the values I tested for 
>above... Seems to be no point to this.
>
>Why??? See below....
> 
>
>>>      if (strcmp(ret,".jhr") != 0)
>> 
>> Will almost certainly go wrong.
>> 
>
>This is why its bad!
>
>So this nested if loop is the /*solution?*/
>
>Seems terribly sloppy to me!

Maybe C is not the right language for your programming style.

>#include <dirent.h> 
>#include <stdio.h> 
>#include <string.h>
>
>int main(void)
>{
>  char hdrfile;
>  int len;
>  const char ch = '.';
>  char *ret;
>  DIR           *d;
>  struct dirent *dir;
>  d = opendir(".");
>  if (d)
>  {
>    while ((dir = readdir(d)) != NULL)
>    {
>     ret = strchr(dir->d_name, ch);
>     //printf("String after |%c| is - |%s|\n", ch, ret);    
>      if (ret)
>      {
>	if (strcmp(ret,".jhr") == 0)
>	{

I don't know why you chose to search for '.' and then check for ".jhr"
when you could have combined everything into a single call to strstr
and searched for ".jhr" directly.  You don't need a sequence of nested
if statements.

>	printf("ret is: |%s|\n",dir->d_name);
>	}
>      }
>      
>     //end while //
>     }
>
>    closedir(d);
>  }
>
>  return(0);
>}

-- 
Remove del for email

[toc] | [prev] | [next] | [standalone]


#100080

FromS390 <s390@tampascanner.info>
Date2016-12-23 16:08 +0000
Message-ID<t_b7A.128432$NC2.33627@fx13.iad>
In reply to#99946
On Tue, 20 Dec 2016 14:38:12 -0800, Barry Schwarz wrote:

> Ignoring the mistake of = when you meant ==,

The above is not code, its just to illustrate a point, which = gets 
across the point.

> your second test is most
> certainly asking about pointers.

Doing 

Me: Is X equal to ABC?

C: Its at bffd8b3c


Me: SO?? 

That is not an answer to my question at all.

I don't care where you store the crap at... If I ask about x, give me x's 
value, or a test against x, or let me divvy up x or what ever....

Why would any one care if x is stored at  bffd8b3c or any where else?


> Maybe C is not the right language for your programming style.

It most certainly is not.... but I am stuck with it, due to this project, 
which I didn't write.

My POC for the functions I am converting are in Python, and don't have to 
have all this hand holding for things. Yes, being interpreted Python is 
probably, mostly likely doing that... 

To be honest.. Is there is something along the lines of

#define DISABLE STUPID POINTERS! JUST GIVE ME VARIABLE VALUES???????

I am guessing not, since probably the way the "standard" functions for 
things work they seem to start looking for this pointer crap versus the 
value.

> I don't know why you chose to search for '.' and then check for ".jhr"
> when you could have combined everything into a single call to strstr and
> searched for ".jhr" directly.  You don't need a sequence of nested if
> statements.

Well, as I've stated, and in the subject... C is *NOT* my thing...."Re: C 
newb " and to be honest with you what this thing can test for and not 
test for in this is a learning in progress thing... 

Honestly, I don't get what its doing as it is... due to this whole 
"pointers" thing... Like I said.... if I query a variable, regardless of 
type, is x some value could be what ever... The answer of Its over there. 
Is well, not an answer! I don't care where in memory its located, or its 
pointed to or the sky is blue for that matter. Is x equal, or what ever 
test I am applying, to the value I gave? YES? NO? Or do a lstr(x,10) or 
what ever.... memory location is not anything I care about.

The c compiler and linker may care about that... not my problem.

-- 
I MADE America Great Again! President Trump! I put a stake in that bitch 
for good!

[toc] | [prev] | [next] | [standalone]


#100083

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2016-12-23 16:34 +0000
Message-ID<8737hebo4f.fsf@bsb.me.uk>
In reply to#100080
S390 <s390@tampascanner.info> writes:
<snip>
> To be honest.. Is there is something along the lines of
>
> #define DISABLE STUPID POINTERS! JUST GIVE ME VARIABLE VALUES???????

No.  Why are you using C?  If you don't want to learn about using
pointers then C is not the right language.  There are lots of other
alternatives, but I don't recall the context of your question so you may
be stuck with C.  If so, you have to bite the bullet and learn about
pointers because almost everything to so with strings is done with
pointers in C.

<snip>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#101037

From"James R. Kuyper" <jameskuyper@verizon.net>
Date2017-01-03 16:16 -0500
Message-ID<45ecb216-892a-87f7-91cf-3763363c49c2@verizon.net>
In reply to#100080
On 12/23/2016 11:08 AM, S390 wrote:
> On Tue, 20 Dec 2016 14:38:12 -0800, Barry Schwarz wrote:
>
>> Ignoring the mistake of = when you meant ==,
>
> The above is not code, its just to illustrate a point, which = gets
> across the point.

No, it does not. "=" is the assignment operator in C, "==" is the 
comparison operator. Using "=" when you mean "==" when discussing C is 
NOT a way to "get across the point".

>> Maybe C is not the right language for your programming style.
>
> It most certainly is not.... but I am stuck with it, due to this project,
> which I didn't write.
>
> My POC for the functions I am converting are in Python, and don't have to
> have all this hand holding for things. Yes, being interpreted Python is
> probably, mostly likely doing that...
>
> To be honest.. Is there is something along the lines of
>
> #define DISABLE STUPID POINTERS! JUST GIVE ME VARIABLE VALUES???????

You can't write any significant amount of C without using pointers, at 
least implicitly using pointers: both arrays and functions are 
implicitly converted into pointers in most contexts. It's difficult, and 
not worth the trouble, to write C code of any significant complexity, 
without making explicit used of pointers. If you're unwilling to learn 
how C pointers work, C is definitely not the language for you.

...
> Honestly, I don't get what its doing as it is... due to this whole
> "pointers" thing... Like I said.... if I query a variable, regardless of

If if weren't for your phobia about pointers, it would be relatively 
easy for us to explain and for you to understand.

[toc] | [prev] | [next] | [standalone]


#100086

FromLew Pitcher <lew.pitcher@digitalfreehold.ca>
Date2016-12-23 12:22 -0500
Message-ID<o3jme7$i0j$1@dont-email.me>
In reply to#99098
On Monday December 12 2016 12:58, in comp.lang.c, "S390"
<s390@tampascanner.info> wrote:

> 
> In looking to build up the blocks of what I want to do for my program, I
> am starting with one piece of that.
> 
> finding all *.jhr in a directory
> 
> So looking over some examples....and building from them I the below, but
> when doing the test to find *.jhr files it seg faults... tried on a
> couple versions of GCC
> 
> /jamtest $ gcc --version
> gcc (Ubuntu/Linaro 4.4.4-14ubuntu5.1) 4.4.5
> 
> @fidodev:~/jamtest$ gcc --version
> gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
> Copyright (C) 2013 Free Software Foundation, Inc.
> 
> I am lost at this point... why the test for the *.jhr file fails and seg
> faults...
> 
> 
> $ cat dirtest.c
> 
> #include <dirent.h>
> #include <stdio.h>
> #include <string.h>
> 
> int main(void)
> {
>   char hdrfile;
>   int len;
>   const char ch = '.';
>   char *ret;
>   DIR           *d;
>   struct dirent *dir;
>   d = opendir(".");
>   if (d)
>   {
>     while ((dir = readdir(d)) != NULL)
>     {
>      ret = strchr(dir->d_name, ch);
>      printf("String after |%c| is - |%s|\n", ch, ret);
>      
>       //if (strcmp(dir->d_name,"TESTJAMNNTPD.jhr") == 0) //
>      if (strcmp(ret,".jhr") != 0)
>         printf("%s\n", dir->d_name);
>     }
> 
>     closedir(d);
>   }
> 
>   return(0);
> 
> This results in:
> 
> @fidodev:~/jamtest$ make dirtest
> cc     dirtest.c   -o dirtest
> @fidodev:~/jamtest$ ./dirtest
> String after |.| is - |..|
> ..
> String after |.| is - |.jlr|
> TESTJAMNNTPD.jlr
> String after |.| is - |.jdx|
> TESTJAMNNTPD.jdx
> String after |.| is - |(null)|
> Segmentation fault (core dumped)

That's not surprising.
[snip]

You're code has several flaws. Let's take a look at the essential parts of
your code.

#include <dirent.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
  int len;
  const char ch = '.';
  char *ret;
  DIR           *d;
  struct dirent *dir;


  d = opendir(".");
  if (d)
/* Good - you don't just assume that opendir() worked */
  {
    while ((dir = readdir(d)) != NULL)
/* Good - you test for the appropriate loop termination condition */
    {
     ret = strchr(dir->d_name, ch);
/* Here, you assume that strchr() found the tested-for character
   What if it hadn't found it? The '.' is not /required/ in filenames
   and there may be some names that have no '.' in them
*/

     printf("String after |%c| is - |%s|\n", ch, ret);
/* Sorry, but <<ret>> /does not/ point to the character /after/ the '.'
   If <<ret>> is valid, it points /to/ the (first) '.' encountered
*/

/* Remember that I pointed out earlier that strchr() might not find the
   tested-for character in the string? So, can you guarantee that the
   value that strchr() returned /under that condition/ is a pointer that
   points to a string? (Hint: reread the description of the strchr() call,
   and look for what it returns under /not found/ conditions.) If strchr()
   returned something other than a pointer to a string, would strcmp() work?
   Would it work /properly/, or would it fail somehow? If so, how?
*/
     if (strcmp(ret,".jhr") != 0)
        printf("%s\n", dir->d_name);
    }

    closedir(d);
  }

  return(0);
}



-- 
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request

[toc] | [prev] | [next] | [standalone]


#100087

FromBartC <bc@freeuk.com>
Date2016-12-23 17:40 +0000
Message-ID<o3jnfl$n11$1@dont-email.me>
In reply to#99098
On 12/12/2016 17:58, S390 wrote:

> finding all *.jhr in a directory

>     while ((dir = readdir(d)) != NULL)
>     {
>      ret = strchr(dir->d_name, ch);
>      printf("String after |%c| is - |%s|\n", ch, ret);
>
>       //if (strcmp(dir->d_name,"TESTJAMNNTPD.jhr") == 0) //


Have a look at the 'fnmatch()' function which will do wildcard matching 
for you. Something like:

    if (fnmatch("*.jhr", dir->d_name, 0)) ...

'man fnmatch' for further details.

-- 
Bartc

[toc] | [prev] | [next] | [standalone]


#100096

FromKeith Thompson <kst-u@mib.org>
Date2016-12-23 14:13 -0800
Message-ID<lnbmw2b8fu.fsf@kst-u.example.com>
In reply to#100087
BartC <bc@freeuk.com> writes:
> On 12/12/2016 17:58, S390 wrote:
>> finding all *.jhr in a directory
>
>>     while ((dir = readdir(d)) != NULL)
>>     {
>>      ret = strchr(dir->d_name, ch);
>>      printf("String after |%c| is - |%s|\n", ch, ret);
>>
>>       //if (strcmp(dir->d_name,"TESTJAMNNTPD.jhr") == 0) //
>
>
> Have a look at the 'fnmatch()' function which will do wildcard matching 
> for you. Something like:
>
>     if (fnmatch("*.jhr", dir->d_name, 0)) ...
>
> 'man fnmatch' for further details.

Be aware that fnmatch() is defined by POSIX, not by ISO C, and it might
not be available on all systems.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#100097

FromLew Pitcher <lew.pitcher@digitalfreehold.ca>
Date2016-12-23 17:22 -0500
Message-ID<o3k7vt$jnt$1@dont-email.me>
In reply to#100096
On Friday December 23 2016 17:13, in comp.lang.c, "Keith Thompson"
<kst-u@mib.org> wrote:

> BartC <bc@freeuk.com> writes:
>> On 12/12/2016 17:58, S390 wrote:
>>> finding all *.jhr in a directory
>>
>>>     while ((dir = readdir(d)) != NULL)
>>>     {
>>>      ret = strchr(dir->d_name, ch);
>>>      printf("String after |%c| is - |%s|\n", ch, ret);
>>>
>>>       //if (strcmp(dir->d_name,"TESTJAMNNTPD.jhr") == 0) //
>>
>>
>> Have a look at the 'fnmatch()' function which will do wildcard matching
>> for you. Something like:
>>
>>     if (fnmatch("*.jhr", dir->d_name, 0)) ...
>>
>> 'man fnmatch' for further details.
> 
> Be aware that fnmatch() is defined by POSIX, not by ISO C, and it might
> not be available on all systems.

I believe that fnmatch() will be available on the system S390 uses; he's
already using the POSIX functions opendir(), readdir(), and closedir().

-- 
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request

[toc] | [prev] | [next] | [standalone]


#100099

FromBartC <bc@freeuk.com>
Date2016-12-23 22:35 +0000
Message-ID<o3k8oo$mp6$2@dont-email.me>
In reply to#100096
On 23/12/2016 22:13, Keith Thompson wrote:
> BartC <bc@freeuk.com> writes:
>> On 12/12/2016 17:58, S390 wrote:
>>> finding all *.jhr in a directory
>>
>>>     while ((dir = readdir(d)) != NULL)
>>>     {
>>>      ret = strchr(dir->d_name, ch);
>>>      printf("String after |%c| is - |%s|\n", ch, ret);
>>>
>>>       //if (strcmp(dir->d_name,"TESTJAMNNTPD.jhr") == 0) //
>>
>>
>> Have a look at the 'fnmatch()' function which will do wildcard matching
>> for you. Something like:
>>
>>     if (fnmatch("*.jhr", dir->d_name, 0)) ...
>>
>> 'man fnmatch' for further details.
>
> Be aware that fnmatch() is defined by POSIX, not by ISO C, and it might
> not be available on all systems.

Since the OP used 'opendir' and 'readdir', it seemed a safe bet that 
'fnname' would be available.


-- 
bartc

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web