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 9217105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:34:03+00:00 2026-06-18T02:34:03+00:00

#include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <dirent.h> #include <string.h> #include <stdlib.h> #include <time.h>

  • 0
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>

struct node
{
    char *s_xmlfile;
    char *s_file_mod_time;
    struct node *link;
};

struct node* head = NULL;

int table(char *);
struct node *getnode();
void readnode(struct node *, char *, char *);
void add_table(struct node **, char *);
void delete_node(struct node**);
void display(struct node *);
char* modification( char*);
const char *getExt (const char *);
void scan_directory (char *);
char* modified_time(char *);

int main(int argc, char *argv[])
{
    printf("\n");
    scan_directory(argv[1]); //enter the directory full path from command line
    display(head);
    return 0;
}

/***scan directory and get all files**/
void scan_directory (char *dir_location)
{
    DIR *dir_ptr;
    struct dirent *file_ptr;     
    chdir(dir_location);
    dir_ptr = opendir (dir_location);
    int dir_len = strlen(dir_location);
    if (dir_ptr != NULL){
        while (file_ptr = readdir (dir_ptr)){
                if( !strcmp((getExt (file_ptr->d_name)), ".xml") ){
                table(file_ptr->d_name);
            }
        }

        (void) closedir (dir_ptr);
    }
    else
        perror ("Couldn't open the directory");

}

/**load only xml file**/
const char *getExt (const char *fspec) 
{
    char *e = strrchr (fspec, '.');
        if (e == NULL)
            e = "";
    return e;
}

/**creating a table to store xml files and
   last file modified time using linked list**/
int table(char *xml_file)
{
    add_table(&head, xml_file);
    //display(head);
}

void add_table(struct node **headptr, char *xml_file)
{
    char *mod_time;
    struct node *loc_head, *last, *newnode;
    loc_head = *headptr;

    mod_time = modified_time(xml_file);
    printf("TIME : %s FILE : %s\n", mod_time, xml_file);
    newnode = getnode();

    newnode -> s_xmlfile = xml_file;
    newnode -> s_file_mod_time = mod_time; 
    newnode -> link = NULL;

    if(loc_head == NULL){
        printf("Debug: Empty table \n");
        loc_head = newnode;
    }
    else{
        last = loc_head;
        while(last ->link != NULL){
        last = last -> link;}
        last->link = newnode;
    }
    *headptr = loc_head;
}

struct node *getnode()
{
    struct node *newnode;
    newnode = (struct node*)malloc(sizeof(struct node));
    return (newnode);
}

void display(struct node *q)
{
    printf("\nDISPLAY\n");
        if(q==NULL){
        //printf("\nEmpty Table.");
        return;
        }
        while(q!=NULL){
        printf("%s\t", q -> s_xmlfile);
        printf("%s\n", q -> s_file_mod_time);
        q=q->link;
        }
    printf("\n");
}

/**identifying last modified time of file**/
char* modified_time(char *xml_file)
{
    struct stat sb;
    char *temp_time;;
    if(stat(xml_file, &sb) == -1)
            perror("stat");
    temp_time = ctime(&sb.st_mtime);
    return temp_time;
}

actual problem is last file time updating for all time field in linked list

Description :
This program will scan the directory for xml file, and get the last updated time of xml file then making entry into linked list.

Actual o/p :

TIME : Mon Jan 28 22:29:42 2013
 FILE : HLM2.xml
Debug: Empty table
TIME : Mon Jan 28 17:41:28 2013
 FILE : HLM1.xml
TIME : Thu Jan 31 22:57:36 2013
 FILE : HLM3.xml

DISPLAY
HLM2.xml    Thu Jan 31 22:57:36 2013

HLM1.xml    Thu Jan 31 22:57:36 2013

HLM3.xml    Thu Jan 31 22:57:36 2013

Expected o/p :

TIME : Mon Jan 28 22:29:42 2013
FILE : HLM2.xml
Debug: Empty table
TIME : Mon Jan 28 17:41:28 2013
 FILE : HLM1.xml
TIME : Thu Jan 31 22:57:36 2013
 FILE : HLM3.xml

DISPLAY
HLM2.xml    Thu Jan 31 22:57:36 2013

HLM1.xml    Mon Jan 28 17:41:28 2013

HLM3.xml    Thu Jan 31 22:57:36 2013

diretory which i giving has 3 xml files
is there any error with my linked list program.

  • 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-06-18T02:34:05+00:00Added an answer on June 18, 2026 at 2:34 am

    It’s the classic “let’s point a pointer to a local char array” problem.

    Instead of:

    newnode -> s_xmlfile = xml_file;
    

    you need:

    newnode -> s_xmlfile = strdup(xml_file);
    

    or:

    int len = strlen(xmlfile);
    char *name = malloc(len+1);
    strcpy(name, xmlfile);
    newnode -> s_xmlfile = name;
    

    Don’t forget to free your newnode->s_xmlfile when you clean up at the end.

    Or make s_xmlfile into char s_xmlfgile[MAX_FILE_NAME_SIZE] and just strcpy(newnode->s_xmlfile, xmlfile); – make sure that MAX_FILE_NAME_SIZE is big enough tho’.

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

Sidebar

Related Questions

#include<stdio.h> #include<unistd.h> #include<stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include<string.h> #include <fcntl.h> void match_pattern(char
#include <stdlib.h> #include <unistd.h> #include <string.h> #include <sys/types.h> #include <stdio.h> int main(int argc, char
#include <stdio.h> #include <dirent.h> #include <sys/types.h> #include <sys/param.h> #include <sys/stat.h> #include <unistd.h> #include <string.h>
#include <stdio.h> #include <dirent.h> #include <sys/types.h> #include <sys/param.h> #include <sys/stat.h> #include <unistd.h> #include <string.h>
#include <unistd.h> #include<stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> #include <string.h> #include
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <string.h> #include <sys/stat.h> #include <fcntl.h>
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <string.h> #include <sys/stat.h> #include <fcntl.h>
Here is my code: #include<stdio.h> #include<stdlib.h> #include<sys/stat.h> #include<sys/types.h> #include<fcntl.h> #include<unistd.h> #include<errno.h> int main(int argc,char
#include <fcntl.h> #include <stdio.h> #include <string.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> int main
#include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<sys/ipc.h> #include<sys/msg.h> #include<sys/types.h> struct mbuffer{ long mtype; int mtext; }stResult; int

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.