linux——3.文件操作
来源:互联网 发布:电信网络字母 编辑:程序博客网 时间:2024/04/27 20:41
/* * file.cpp: for linux file methods */#include <fcntl.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <stdio.h>#include <dirent.h>#include <iostream>using namespace std;#define UNIT_TEST 1// file classclass File{public: File() { m_fd = -1; } ~File() { close(); } /* * system lib call */ // open // oflags: O_RDONLY, O_WRONLY, O_RDWR; O_APPEND, O_TRUNC, O_CREAT, O_EXCL // mode: S_IRUSR, S_IWUSR, S_IXUSR.. bool open(const char *path, int oflags = O_RDWR, int mode = S_IRUSR | S_IWUSR) { m_fd = ::open(path, oflags, mode); return m_fd != -1; } // close void close() { if (m_fd != -1) { ::close(m_fd); m_fd = -1; } } // write size_t write(const void *buf, size_t nbytes) { if (m_fd != -1) { return ::write(m_fd, buf, nbytes); } return -1; } // read size_t read(void *buf, size_t nbytes) { if (m_fd != -1) { return ::read(m_fd, buf, nbytes); } return -1; } // copy static bool copy(const char *path, const char *pathout) { File in; if (!in.open(path, O_RDONLY)) { return false; } File out; if (!out.open(pathout, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) { return false; } char buf[1024]; int nread; while ((nread = in.read(buf, sizeof(buf))) > 0) { out.write(buf, nread); } return true; } // seek off_t lseek(off_t offset, int whence = SEEK_SET) { if (m_fd != -1) { return ::lseek(m_fd, offset, whence); } return -1; } /* * std io lib */ // fopen, fclose, fread, fwrite, fflush, fsee,, fgetc, fputc, fprintf.. /* * maintain file and folder */ // change file or folder access priority static int chmod(const char *path, mode_t mode) { return ::chmod(path, mode); } protected: int m_fd;};// directoryclass Directory{public: Directory() { m_dir = NULL; } ~Directory() { close(); } // make dir static int mkdir(const char *path, mode_t mode) { return ::mkdir(path, mode); } // remove dir // only if the dir is empty static int rmdir(const char *path) { return ::rmdir(path); } // change dir static int chdir(const char *path) { return ::chdir(path); } // get current work directory static char *getcwd(char *buf, size_t size) { return ::getcwd(buf, size); } // open dir bool open(const char *name) { m_dir = ::opendir(name); return m_dir != NULL; } // read dir // dirent: d_ino inode num, d_name file name dirent* read(DIR *dir = NULL) { if (!dir) dir = m_dir; if (dir) return readdir(dir); return NULL; } void close() { if (m_dir) { closedir(m_dir); m_dir = NULL; } } long int tell(DIR *dir = NULL) { if (!dir) dir = m_dir; if (dir) return telldir(dir); return -1; } void seek(DIR *dir = NULL, long int loc = 0) { if (!dir) dir = m_dir; if (dir) seekdir(dir, loc); } // scan static void scan(const char *path) { // open dir Directory dir; if (!dir.open(path)) { cout << "open dir failed:" << path << endl; return; } // change dir chdir(path); // scan dir struct dirent *entry; struct stat statbuf; while ((entry = dir.read()) != NULL) { lstat(entry->d_name, &statbuf); if (S_ISDIR(statbuf.st_mode)) { // found a dir, but ignore . and .. if (!strcmp(".", entry->d_name) || !strcmp("..", entry->d_name)) { continue; } scan(entry->d_name); } else { cout << entry->d_name << endl; } } chdir(".."); dir.close(); }protected: DIR* m_dir;};#if UNIT_TEST == 1int main(){ File file; // create open.txt if (!file.open("open.txt", O_RDWR | O_CREAT)) { cout << "create file open.txt error.\n"; } else { // write char buf[] = "this is adfan\nit's true\n"; file.write(buf, sizeof(buf)); file.close(); } // open if (!file.open("open.txt")) { cout << "open file open.txt error.\n"; } else { char buf[256] = {0}; file.read(buf, 256); cout << buf << endl; } // close file file.close(); // copy File::copy("open.txt", "open_out.txt"); char dir[256]; cout << Directory::getcwd(dir, 256) << endl; // scan Directory::scan(dir);}#endif
0 0
- linux——3.文件操作
- Linux——文件操作
- linux程序设计—文件操作
- Linux——Linux 文件基本操作
- linux命令——文件操作
- linux系统调用——文件操作
- 《Linux程序设计》——文件操作
- Linux编程——文件 IO操作
- Linux编程——文件操作
- linux编程——文件操作1
- linux C —— 文件操作函数
- linux命令——文件目录操作
- Linux编程——文件操作
- Linux程序设计——文件操作
- Linux程序设计——文件操作(底层文件访问)
- linux系统编程文件——文件操作总结(3)
- Linux文件管理(2)——文件操作
- linux学习:c语言文件操作—读文件
- 习题四
- thinkphp 自动运行方法
- 关于B-K tree与k-d tree一些自己想法
- 4.21-4.22
- Discrete Mathematics and Its applications - 离散数学及其应用 读书笔记 ( 一 )
- linux——3.文件操作
- 编程珠玑(第二版)读书笔记【1、2】
- ZOJ-3647 Gao the Grid
- Jedis 增删改查
- CSS3属性text-overflow
- VIM会话-窗口布局
- 实验三
- jquery frre
- uva 11997 k个最小和(优先队列实现多路归并)题解