Linux Experiment report Two--file operation (cont.)

Source: Internet
Author: User

#include <stddef.h>

#include <stdio.h>

#include <sys/types.h>

The file where the #include <sys/stat.h>//stat function is located

#include <dirent.h>

int main (void)

{

DIR *DP;

struct Dirent *ep;

struct STAT st;

Char dirp[50];

printf ("Please input a dir name:/n");

scanf ("%s", &DIRP); Read Directory Name

Dp=opendir ("Dirp"); Open the given directory

printf ("filename:/ttype:/tpermission/taccesstime/tlastmodtime/tsize/t");

if (dp!=null)//If the directory opened successfully, then the operation is performed.

{

while (ep = Readdir (DP))//Read the cycle of each directory entry

{

if (ep->d_name[0]!= '. ') To determine if the first character of the file name is '. ', if it is, to indicate that it is an implied file, we do not move, otherwise operation

{

Use the STAT function to open the file information, the first parameter is the path of the file, the second parameter holds the information of the file

if (stat (ep->d_name,&st)!=-1)/Read success

{

printf ("%s/t", ep->d_name);

if ((ST.ST_MODE&S_IFMT) ==s_ifdir)//Determine the type of file

printf ("directory/t"); Directory

else if ((ST.ST_MODE&S_IFMT) ==s_ifblk)//block file

printf ("Block special file/t");

else if ((ST.ST_MODE&S_IFMT) ==S_IFCHR)//special character file

printf ("character Special file/t");

else if ((ST.ST_MODE&S_IFMT) ==s_ifreg)//Normal file

printf ("Ordinary file/t");

else if ((ST.ST_MODE&S_IFMT) ==S_IFIFO)//pipe file

printf ("Pipefile file/t");

printf ("%o/t", st.st_mode&0x1ff); Permissions for files

printf ("%15s/t", CTime (St.st_atime)); File creation time

printf ("%15s/t", CTime (St.st_mtime)); File Last Modified Time

printf ("%ld/n", st.st_size); Size of File

}

}

}

Closedir (DP)//Close Directory

}

Else

Puts ("couldn ' t open the directory./n"); Output cannot open path when open is unsuccessful

return 0;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.