Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

Directory & File Handling

Topics
• Directory
– Current Path
– Change Path
– List Directory
– Delete Files and Directories
• File (Text & Binary)
– Open File
• File modes
– Read/Write
– Seek
Working with Directories
• Linux Command – mkdir
– pwd , cd , ls , rm – rmdir
– mkdir , rmdir – remove
• System calls used in C/C++ – rename
– getcwd • Links
– chdir – link
– opendir
– symlink
– readdir
– readlink
– closedir
– rewinddir – unlink
Working with Directories
System calls used in C/C++
1. char * getcwd (char *buffer, size_t size)
2. DIR * opendir (const char *dirname)
3. struct dirent * readdir (DIR *dirstream)
4. int closedir (DIR *dirstream)
5. void rewinddir (DIR *dirstream)
6. int remove (const char *filename)
7. int rename (const char *oldname, const char *newname)
8. int mkdir (const char *filename, mode_t mode)
9. int rmdir (const char *filename)
Working with Directories
System calls used in C/C++

Links (hard, soft/symbolic)


1. int link (const char *oldname, const char *newname)
2. int symlink (const char *oldname, const char *newname)
3. int readlink (const char *filename, char *buffer, size_t size)
4. int unlink (const char *filename)
struct dirent (Directory Entry)
• char d_name[]
• ino_t d_fileno
• unsigned char d_namlen
• unsigned char d_type
– DT_UNKNOWN
– DT_REG regular file
– DT_DIR directory
– DT_FIFO named pipe
– DT_SOCKlocal-domain socket
– DT_CHR character device
– DT_BLK block device
Example : pwd - Show Current Path

#include<stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <sys/types.h>
#include <dirent.h>
int main(void) Example : Getting Current Path

{
int size = 100 ;
char cwd[100]; // = (char*) malloc(size);
getcwd(cwd,size);
printf("%s\n",cwd);
}
Example : ls - Display directory list
DIR *dp;
struct dirent *ep;

dp = opendir ("./");
if (dp != NULL)

{ while (ep = readdir (dp))


{ char type[10];
if(ep->d_type==DT_DIR) strcpy(type,"Dir"); else strcpy(type,"File");

printf("%s : %s \n",type, ep->d_name);


}

closedir (dp);
}
else puts ("Couldn't open the directory.");
}
Programming Assignment
• Write C/C++ programs for the following shell
commands
– pwd
– cd
– ls show all folders/files in the hierarchal/tree form
– chmod / chown
– mkdir
– rm ( remove all files and subdirectories in the given path)
Files and Their Metadata
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int stat (const char *path, struct stat *buf);


int fstat (int fd, struct stat *buf);
int lstat (const char *path, struct stat *buf);
struct stat
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* permissions */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size in bytes */
blksize_t st_blksize; /* blocksize for filesystem I/O */
blkcnt_t st_blocks; /* number of blocks allocated */
time_t st_atime; /* last access time */
time_t st_mtime; /* last modification time */
time_t st_ctime; /* last status change time */
};
Rerferences
File System Interface
http://www.chemie.fu-berlin.de/chemnet/use/info/libc/libc_9.html

Input/Output on Streams
http://www.chemie.fu-berlin.de/chemnet/use/info/libc/libc_7.html

You might also like