#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;
}
实际问题是链接列表中所有时间字段的最后文件时间更新
描述: 该程序将扫描目录中的xml文件,并获取xml文件的最后更新时间,然后进入链表。
实际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
预期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我给出了3个xml文件 我的链接列表程序有任何错误。
答案 0 :(得分:1)
这是经典的“让我们指出一个指向本地字符数组的指针”问题。
而不是:
newnode -> s_xmlfile = xml_file;
你需要:
newnode -> s_xmlfile = strdup(xml_file);
或:
int len = strlen(xmlfile);
char *name = malloc(len+1);
strcpy(name, xmlfile);
newnode -> s_xmlfile = name;
最后清理时不要忘记释放newnode->s_xmlfile
。
或者s_xmlfile
进入char s_xmlfgile[MAX_FILE_NAME_SIZE]
而strcpy(newnode->s_xmlfile, xmlfile);
- 确保MAX_FILE_NAME_SIZE足够大。