Linux file IO directory operations and file attributes

Source: Internet
Author: User

////////////////////////////////////////////////
Exercise: Implement list all file attributes in a directory (file size, file last modified time, filename)
The directory name is passed in by the parameter./dir/home/linux
#include <sys/stat.h>
#include <dirent.h>
#include <stdio.h>
int main (int argc, char *argv[])
{
DIR *DP; Define a struct variable to open the file directory and return a pointer to the DIR struct
DP = Opendir (argv[1]);
struct Dirent *ep; Used to save a file This file contains the names of other files and pointers to information related to those files
EP = Readdir (DP); Readdir, read a file from the directory

while (ep! = NULL)
{
if (ep->d_name[0]! = '. ')
{
Char path[100] = {0};
struct STAT s;
sprintf (Path, "%s/%s", argv[1], ep->d_name);///home/linux/a.txt
int ret = stat (path, &s);
if (ret >= 0)
{
printf ("%s:%d:%s\n", Ep->d_name, (int) s.st_size,asctime (localtime (&s.st_atime)));
}
}
EP = Readdir (DP);
}
}


/////////////////////////////////////

Linux file IO directory operations and file attributes

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.