Hi everyone!

I am making a program in C where the user will input a month then the program prints out all the names of files created in that month. Right now I'm having trouble in printing the names of files. Here's a portion of my code. Can you help me figure out what's wrong with it?

int main (void)
{
    int month;
    scanf("%d", month);  



    DIR *da; 

    struct dirent *ep;  

    time_t temp;
    struct tm *foo = localtime(&temp); 
    struct stat attrib;  

  da = opendir ("./"); 
if (da != NULL) 
{
 while (ep = readdir (da)) 
   while ((ep = readdir (da)) != NULL)
   {
    const size_t len = strlen(ep->d_name);   

    if (len > 4                    &&
        ep->d_name[len - 4] == '.' &&
        ep->d_name[len - 3] == 't' &&
        ep->d_name[len - 2] == 'x' &&
        ep->d_name[len - 1] == 't') {

            stat(ep->d_name, &attrib); 
            foo = localtime(&(attrib.st_mtime));
            month = foo->tm_mon + 1;

            if(month == 1) {
                printf ("%s\n", ep->d_name);
            }

            else if(month == 2) {
                printf ("%s\n", ep->d_name);
            }

Can you help me figure out what's wrong with it?

What problem are you having? Does it not compile? ...not run? ...run but not produce the result you expect?

It runs but it doesn't print out the files created on the month that I inputted.

I edited my code. Please take a look at it.

    DIR *da; 
    struct dirent *ep;  
    struct stat attrib;  

    da = opendir ("./"); 
    if (da != NULL)  
    {
        while (ep = readdir (da)) 
        {
        while ((ep = readdir (da)) != NULL) 
        {
            const size_t len = strlen(ep->d_name);    

                if (len > 4                    && 
                ep->d_name[len - 4] == '.' &&
                ep->d_name[len - 3] == 'T' &&
                ep->d_name[len - 2] == 'X' &&
                ep->d_name[len - 1] == 'T') {

                    stat(ep->d_name, &attrib); 

                    strftime(month, MAX_SIZE, "%m", localtime(&(attrib.st_ctime)));

                    comp = strcmp(month,input);
                    if(comp == 0) {
                        printf ("%s\n", ep->d_name);
                    }   
                } 
           } 
        closedir (da);
        }    
    } 

Please post the entire program, not fragments. If it seems too long for pasting into a code block, make it an attachment.

Hint ...

// dirlist.c
// display information about files
// works with Pelles C and CodeBlocks

#include <stdio.h>
#include <io.h>     // _findfirst()
#include <time.h>   // ctime()

char *get_month(char *time_created);

int main(int argc, char **argv)
{
  // uses working directory if no args given
  char *spec = (argc > 1) ? argv[1] : "*.*";
  struct _finddata_t fd;
  long handle;
  char *time_created;
  char *month;

  handle = _findfirst(spec, &fd);
  if (handle == -1) {
    printf("Unable to find any files matching the selection \"%s\"\n", spec);
    return 1;
  }
  do {
    printf("%10u  %.*s  %c%c%c%c%c  %s\n",
      fd.size,
      24,
      // last modified
      ctime(&fd.time_write),
      (fd.attrib & _A_SUBDIR) ? 'd' : '-',
      (fd.attrib & _A_RDONLY) ? 'r' : '-',
      (fd.attrib & _A_HIDDEN) ? 'h' : '-',
      (fd.attrib & _A_SYSTEM) ? 's' : '-',
      (fd.attrib & _A_ARCH) ?   'a' : '-',
      fd.name);
  } while (_findnext(handle, &fd) == 0);
  printf("-----------------------------------\n");

  // reset handle
  handle = _findfirst(spec, &fd);
  do {
      time_created = ctime(&fd.time_create);
      printf("Time created = %.*s  %s\n", 24,
             time_created, fd.name);
  } while (_findnext(handle, &fd) == 0);
  printf("----------------------------------\n");
  _findclose(handle);

  month = get_month(time_created);
  printf("%s", month);

  getchar();  // wait
  return 0;
}

// extract month from eg.
// Sun Jun 08 09:05:03 2014
char *get_month(char *time_created)
{
  char *month;

  month[0] = time_created[4];
  month[1] = time_created[5];
  month[2] = time_created[6];
  month[3] = '\0';

  return month;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.