Linux C Programming: folder operations

Source: Internet
Author: User
Tags readable

To create a directory:

To create a directory with the mkdir function:

mkdir (const char *pathname, mode_t mode)

The parameter mode has the following combinations:

S_isuid 04000 File execution when setting user ID(set User-id on execution) bit

S_isgid Setting the group ID(setgroup-id on execution) bit When the 02000 file is executed

S_ISVTX 01000 File Save Body ( sticky bit sticky) bit

S_irwxu 00700 file owner has read, write, execute permissions

S_irusr(s_iread) 00400 file owner with readable permissions

S_iwusr(s_iwrite)00200 file owner with writable permission

S_ixusr(s_iexec) 00100 file owner with executable permissions

S_IRWXG 00070 User group with read, write, execute permissions

S_IRGRP 00040 User group with readable permissions

S_IWGRP 00020 User group with writable permissions

S_IXGRP 00010 user Group with executable permissions

S_irwxo 00007 Other users have read, write, execute permissions

S_iroth 00004 Other users have Read permissions

S_iwoth 00002 Other users with writable permissions

S_ixoth 00001 Other users with executable permissions

The code is as follows

#include <unistd.h>

#include <sys/stat.h>

void Make_dir_function () {

const char *pathname= "/home/zhf/c_test";

mkdir (pathname,0744);

}

Read directory:

DIR *opendir (const char *pathname); Gets a list of the files and directories under the path subdirectory and returns null if path is a file

the prototype of the DIR struct is:struct_dirstream in a linux system:typedef struct __DIRSTREAM DIR;

struct __dirstream
{
void *__fd;   /* ' struct HURD_FD ' pointer for descriptor. */
Char *__data;   /* Directory block. */
int __entry_data;   /* Entry number ' __data ' corresponds to. */
Char *__ptr;   /* Current pointer into the block. */
int __entry_ptr;   /* Entry number ' __ptr ' corresponds to. */
size_t __allocation;   /* Space allocated for the block. */
size_t __size;   /* Total valid data in the block. */
__libc_lock_define (, __lock)/* Mutex lock for this structure. */
};

struct Dirent *readdir (DIR *DP); Reads the list of Opendir return values. Returns a pointer to the dirent structure, and the members of the dirent struct are as follows:

struct dirent

  {

  long D_ino; */inode number Index Node Number */ 

  off_t d_off;/* Offset to this dirent offset in the catalog file */ 

  unsigned short d_reclen;/* length of this d_name Long File name */ 

  unsigned char d_type; * /the type of D_name File Type */ 

  Char d_name [name_max+1];/* file name (null-terminated) file name, maximum 255 character */ 

}

The code is as follows:

void Read_files_in_dir () {

DIR *dirptr=null;

struct Dirent *entry;

Dirptr=opendir ("/home/zhf/c_prj");

while (Entry=readdir (dirptr)) {

printf ("The file name is%s\n", entry->d_name);

}

Closedir (DIRPTR);

}

Operation Result:

The file name is FUNCTION.O

The file name is chapter3.c

The file name is MAIN.C

The file name is File.hole

The file name is Chapter3

The file name is File_function

The file name is FILE_FUNCTION.C

The file name is MAIN.O

The file name is offer_test.c

The file name is.

The file name is FUNCTION.C

The file name is:

The file name is main

The file name is Offer_test

Directory modification:

Each process has a current working directory, and the process calls the chdir or fchdir function to change the current working directory

The following code modifies the working directory to /HOME/ZHF/C_PRJ

void Change_dir_function () {

Char *ptr;

size_t size;

ChDir ("/home/zhf/c_prj");

}

But when we use PWD to query in the shell to find the working directory or/HOME/ZHF, modifying the working directory does not take effect

[Email protected]:/home/zhf# pwd

/home/zhf

This is because each program runs in a separate process, and the shell's current working directory does not change as the program calls ChDir. So in order to change the shell process's own working directory, the shell should call the ChDir function directly. So how do you change the working directory in your code to see it? This will require the use of the GETCWD function.

Char *getcwd (char *buf,size_t size) returns BUF if successful, returns NULL if an error occurs

The code is as follows

void Change_dir_function () {

Char *ptr;

size_t size;

ChDir ("/home/zhf/c_prj");

Ptr= (char *) malloc (sizeof (size));

GETCWD (ptr,size);

printf ("The Work Directory is%s\n", PTR);

}

The Work directory IS/HOME/ZHF/C_PR

Linux C Programming: folder operations

Related Article

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.