सी

2015-04-09 6 views
5

में फ़ाइल और प्रिंट फ़ाइल विशेषताओं के माध्यम से लूप I में प्रोग्रामिंग के लिए बहुत नया है। मुझे इस प्रोग्राम को फ़ोल्डर में सभी फ़ाइलों के माध्यम से लूप करने और प्रत्येक फ़ाइल के लिए इन विशेषताओं को प्रिंट करने की आवश्यकता है। इस बिंदु पर यह सिर्फ फ़ोल्डर के गुणों को मुद्रित कर रहा है।सी

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

int main(int argc, char *argv[]) 
{ 
    DIR *dp; 
    struct stat file_stats; 

    if (argc != 2) { 
     fprintf(stderr, "Usage: fstat FILE...\n"); 
     return EXIT_FAILURE; 
    } 

    if ((stat(argv[1], &file_stats)) == -1) { 
     perror("fstat"); 
     return EXIT_FAILURE; 
    } 

    dp = opendir("./"); 
    if (dp == NULL) { 
     perror("couldn't open directory"); 
     return EXIT_FAILURE; 
    } 

    while (readdir(dp)) { 
     printf("filename: %s\n", argv[1]); 
     printf(" device: %lld\n", 
       file_stats.st_dev); 
     printf(" protection: %o\n", 
       file_stats.st_mode); 
     printf(" number of hard links: %d\n", 
       file_stats.st_nlink); 
     printf(" user ID of owner: %d\n", 
       file_stats.st_uid); 
     printf(" group ID of owner: %d\n", 
       file_stats.st_gid); 
     printf(" device type (if inode device): %lld\n", 
       file_stats.st_rdev); 
     printf(" total size, in bytes: %ld\n", 
       file_stats.st_size); 
     printf(" blocksize for filesystem I/O: %ld\n", 
       file_stats.st_blksize); 
     printf(" inode number: %lu\n", 
       file_stats.st_ino); 
     printf(" time of last access: %ld : %s", 
       file_stats.st_atime, 
       ctime(&file_stats.st_atime)); 
     printf(" time of last change: %ld : %s", 
       file_stats.st_ctime, 
       ctime(&file_stats.st_ctime)); 
     closedir(dp); 
    } 

    return EXIT_SUCCESS; 
} 

मैं मैं जबकि लूप में struct बढ़ने की जरूरत है लगता है, लेकिन जब मुझे लगता है कि संकलक कहते हैं "अघोषित file_stats।"

उत्तर

1

आप

if((stat(ep->d_name, &file_stats)) == -1) { 
    perror("fstat"); 
    return 1; 
    } 

जबकि पाश अंदर बुलाना चाहिए।

+0

क्या आपने देखा है कि वह भी '' invokes पाश में closedir() है? 'मुख्य') से '0' वापस करने से पहले, बाहर नहीं होना चाहिए? –

+1

सही! दुर्भाग्यवश मेरे पास परीक्षण करने के लिए पॉज़िक्स मशीन नहीं है, इसलिए मैंने केवल पहली त्रुटि पर ध्यान दिया। असल में 'printf ("फ़ाइल नाम:% s \ n", argv [1]); '' printf ("फ़ाइल नाम:% s \ n", ep-> d_name) द्वारा प्रतिस्थापित किया जाना चाहिए;' और 'dp = opendir ("./"); 'दिखता है 'डीपी = opendir (argv [1]);' मोड समझदार होना। –

+0

आप बिल्कुल सही हैं! मैंने आपके परिवर्तनों के साथ प्रोग्राम (मेरी लिनक्स मशीन पर) की जांच की है और अब यह सही तरीके से काम कर रहा है। एक पूरी तरह से सही काम करने के कोड के लिए इसे मेरे जवाब में जोड़ देंगे। –

3

वैलेंटाइन के उत्तर के अतिरिक्त, आपको लूप से closedir() स्थानांतरित करना चाहिए।

अद्यतन: भी आप वास्तविक फ़ाइल के बारे में जानकारी प्राप्त करने के लिए stat(ep->d_name, ...) साथ stat(argv[1], ...) को बदलने के लिए की जरूरत है। लेकिन इससे पहले आपको argv[1] निर्देशिका दर्ज करें (chdir() सिस्टम कॉल का उपयोग करके)।

पूरी तरह से काम कर रहे कोड:

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

int main(int argc, char *argv[]) 
{ 
    DIR *dp; 
    struct stat file_stats; 
    struct dirent *ep; 
    int ret = EXIT_SUCCESS; 
    int ret2; 

    if (argc != 2) { 
     fprintf(stderr, "Usage: %s FILE...\n", argv[0]); 
     return EXIT_FAILURE; 
    } 

    dp = opendir(argv[1]); 
    if (dp == NULL) { 
     perror("Couldn't open directory"); 
     return EXIT_FAILURE; 
    } 

    ret2 = chdir(argv[1]); 
    if (ret2 == -1) { 
     perror("Unable to change directory"); 
     ret = EXIT_FAILURE; 
     goto out1; 
    } 

    while ((ep = readdir(dp))) { 
     printf("filename: %s\n", ep->d_name); 

     if ((stat(ep->d_name, &file_stats)) == -1) { 
      perror("fstat"); 
      ret = EXIT_FAILURE; 
      goto out2; 
     } 

     printf(" device: %lld\n", 
       file_stats.st_dev); 
     printf(" protection: %o\n", 
       file_stats.st_mode); 
     printf(" number of hard links: %d\n", 
       file_stats.st_nlink); 
     printf(" user ID of owner: %d\n", 
       file_stats.st_uid); 
     printf(" group ID of owner: %d\n", 
       file_stats.st_gid); 
     printf(" device type (if inode device): %lld\n", 
       file_stats.st_rdev); 
     printf(" total size, in bytes: %ld\n", 
       file_stats.st_size); 
     printf(" blocksize for filesystem I/O: %ld\n", 
       file_stats.st_blksize); 
     printf(" inode number: %lu\n", 
       file_stats.st_ino); 
     printf(" time of last access: %ld : %s", 
       file_stats.st_atime, 
       ctime(&file_stats.st_atime)); 
     printf(" time of last change: %ld : %s\n", 
       file_stats.st_ctime, 
       ctime(&file_stats.st_ctime)); 
    } 

out2: 
    ret2 = chdir(".."); 
    if (ret2 == -1) { 
     perror("Unable to change directory back"); 
     ret = EXIT_FAILURE; 
     goto out1; 
    } 

out1: 
    closedir(dp); 
    return ret; 
} 
+0

यह लगभग काम करता है। जब मैं कमांड ./program फ़ाइलों के साथ चलाता हूं तो यह केवल "फाइल" निर्देशिका के बारे में जानकारी प्रिंट कर रहा है। तो मुझे 8 बार की तरह मुद्रित एक ही जानकारी मिल रही है। क्या मैं प्रोग्राम गलत तरीके से चला रहा हूं। – MikeT

+0

@ माइकट अच्छा पकड़ो! ऐसा इसलिए है क्योंकि आप 'stat (argv [1], ... 'कर रहे हैं, इसलिए आप मूल रूप से' argv [1] 'में निर्देशिका के बारे में जानकारी प्राप्त कर रहे हैं। मैं इसे अपने उत्तर में ठीक कर रहा हूं, मुझे एक मिनट दें। –

+0

@ माइकट, ठीक है, मैंने अपने जवाब में कोड तय किया है, अब यह सही काम कर रहा है। –