linux--3. File operations

Source: Internet
Author: User
Tags strcmp

/* * File.cpp:for Linux file methods */#include <fcntl.h> #include <sys/types.h> #include <sys/stat.h& GT, #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) {RET        urn:: 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) {Retur        N:: 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//If the dir is empty static int rmdir (const char *path) {return:: RmDir (Pat    h);    }//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, Siz    e);        }//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 <&Lt    "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, n) << Endl; Scan Directory::scan (dir);} #endif

linux--3. File operations

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.