Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6715471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:37:06+00:00 2026-05-26T08:37:06+00:00

Can some on help me out here? I am trying check each directory entry

  • 0

Can some on help me out here? I am trying check each directory entry if a letter exists in its name. Obviously it’s not working. My main question is am I using the memchr correctly in using the namelist[n]->d_name as a memory?

#include <dirent.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

  int main(void)
  {
       struct dirent **namelist;
       int n;

       n = scandir(".", &namelist, 0, alphasort);
       if (n < 0)
           perror("scandir");
       else 
       {
    char * search;
          while (n--) {
        search= (char*) memchr(namelist[n]->d_name,'a',(sizeof(namelist[n]->d_name)));
        if(search !=NULL){
                printf("%s\n", namelist[n]->d_name);
                        free(namelist[n]);
            }
    char * search;
    }
          free(namelist);
       }
   }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:37:07+00:00Added an answer on May 26, 2026 at 8:37 am

    That code actually works for me (under Win7/CygWin):

    pax$ ./qq
    xyzzyaf
    xyzzyae
    xyzzyad
    xyzzyac
    xyzzyab
    xyzzyaa
    .bashrc
    .bash_profile
    .bash_history
    
    pax$ ls -ad *a* .*a*
    .bash_history  .bash_profile  .bashrc  xyzzyaa
    xyzzyab        xyzzyac        xyzzyad  xyzzyae
    xyzzyaf
    

    But why are you using memchr? The d_name field is a C-style string, as evidenced by the fact that you can perform a printf("%s\n",...) on it.

    You should be using strchr for that. Using memchr may search beyond the end of the string, possibly giving false results if it finds a in any junk following that.


    If that doesn’t help you out, then you need to define the phrase “obviously it’s not working”. In other words, what are all the files in the directory and what output are you getting? That will greatly assist in the resoultion of this problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hopefully someone can help out here; I am trying to write a function which
can some on help me on how i can reference images i have all
Hi can some body help me how to call process.existed() method in c#? Actually
Can I get some help posting across different pages from a custom control? I've
I read the SDK document, but can not understand some details, need some help.
I need some help modifying the Webform Module so that it can work for
Are there some help resources, or can anyone give me a brief Idea how
I wonder if someone can help shed some light on this: I drop a
Can anyone suggest some good browser add-on tools/extensions to help with development? I have
Can anybody help me with retrieving some elements from the following example text: sdfaasdflj

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.