2013-01-31 104 views
-9
#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文件 有我的鏈表程序的任何錯誤。

+2

我們爲什麼要糾正錯誤? – StoryTeller

+0

多數民衆贊成在這裏一個命令到人民解放軍? –

+1

如果你看看**標籤,他試圖用粗體設置整個問題(或命令)...... – gustavodidomenico

回答

1

這是經典的「讓我們指向一個本地char數組的指針」問題。

相反的:

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_xmlfilechar s_xmlfgile[MAX_FILE_NAME_SIZE]而只是strcpy(newnode->s_xmlfile, xmlfile); - 確保MAX_FILE_NAME_SIZE足夠大。

+0

仍然我得到相同的錯誤 – sujin

+0

發佈您的更新代碼! –

+0

你是對的。問題通過你的答案得到了解決 – sujin

相關問題