File I/O practices (1)--Basic API

Source: Internet
Author: User

What is I/O

Input/output is the process of copying data between memory and peripherals:

Device--Memory: input operation

Memory, device: output operation


Advanced I/O: standard I/O library functions provided by ANSI C become advanced I/O, also known as buffered I/O;

Low I/o: Linux the system calls provided ,  usually also known as a non-buffered I/O;

File Descriptor

For the Linux kernel, all files or devices correspond to a file descriptor (the design philosophy of Linux: All documents), which simplifies the complexity of system programming;

When a file is opened/created, the kernel returns a file descriptor (a non-negative integer) to the process. Subsequent operation of the file can only be done through the file descriptor, the kernel records information about the open file;

When a process is started, 3 files are opened by default, standard input (0, Stdin_fileno), standard output (1, Stdout_fileno), standard error output (2, Stderr_fileno), and these constants are defined in the Unistd.h header file;

wherein, the file descriptor is basically corresponding to the file description pointer (file*) one by one, such as the file descriptor 0,1,2 corresponds to stdin, stdout, stderr;

Conversion of a file pointer to a file descriptor

Fileno: Converting a file pointer to a file descriptor

int Fileno (FILE *stream);

Fdopen: Converting a file descriptor to a file pointer

FILE *fdopen (int fd, const char *mode);

Example int main () {    cout << fileno (stdin) = "<< Fileno (stdin) << Endl;    cout << "Fileno (stdout) =" << Fileno (stdout) << Endl;    cout << "Fileno (stderr) =" << Fileno (stderr) << Endl;    return 0;}

file I/O API

1.open

#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>int open (const char *pathname, int flags); int open (const char *pathname, int flags, mode_t mode);

Parameters:

Pathname: File name, can contain [absolute/relative] path name;

Flags: File open mode;

Mode: Used to specify access rights to the file owner, the file user group, and other users in the system;

Note: newmode = mode & ~umask

Flags common values


Example 1int Main () {    int fd = open ("Test.txt", o_rdonly);    if (fd = =-1)    {        cerr << "File open error, errno =" << errno <<             "\nstrerror:" << stre Rror (errno) << Endl;        Perror ("perror");        Exit (exit_failure);    }    cout << "File Open success" << Endl;}
Example 2inline void Err_exit (std::string message) {    perror (message.c_str ());    Exit (exit_failure);} int main () {    umask (0000);    int fd = open ("Test.txt", o_rdwr| O_creat| O_EXCL, 0666);    if (fd = =-1)        err_exit ("File open Error");    else        cout << "File descriptor =" << fd << Endl;}

Attached

(1). Umask API

//Change umask value

mode_t umask (mode_t mask);

(2). ulimit-a

View the various restrictions in the system;

where-N: View the maximum number of files that a process can open

(3). Cat/proc/sys/fs/file-max

View the maximum number of open files that a system can support (this number is related to memory size)


2.close

#include <unistd.h>int close (int fd);

Closes the file descriptor, allowing the file descriptor to be re-used

3.read

ssize_t Read (int fd, void *buf, size_t count);

return value:

Error: 1

Reach end of File: 0

Success: Returns the number of bytes copied from the file to the specified buffer

4.wirte

ssize_t Write (int fd, const void *buf, size_t count);

return value:

Error: 1

Not doing anything: 0

Success: Returns the number of bytes successfully written to the file

Attention:

Write returns greater than 0 o'clock, and does not mean that the contents of the BUF have been written to a file on disk, it simply means that the data in BUF has been copied to the appropriate kernel buffer. To implement a file that actually "flushes" the contents of the buffer to the disk, you need to call the Fsync function;

int fsync (int fd);

It synchronizes the contents of the kernel buffer that have not yet been written to disk into the file system;

    In fact, you can specify the synchronization option when the Open is called: O_sync &NBSP; o_sync the file is opened for synchronous i/o.   any   write (2) s  on  the  resulting  file  descriptor  will block the calling process until the data has been  Physically written to the underlying hardware.

Write waits until the content of the BUF is actually written to the disk before it is actually returned;

Example: with O_SYNC option int main (int argc, char *argv[]) {    if (argc < 3)    {        cerr << "Usage:" << argv[0] << "src dest" << Endl;        Exit (exit_failure);    }    int infd = open (argv[1], o_rdonly);    if (INFD = =-1)        err_exit ("File o_rdonly error");    int outfd = open (argv[2], o_wronly| O_creat| o_trunc| O_sync, 0666);    if (outfd = =-1)        err_exit ("File o_wronly error");    Char buf[1024];    int readbytes, writebytes;    while (readbytes = Read (INFD, buf, sizeof (BUF))) > 0)    {        writebytes = write (Outfd, buf, readbytes);        cout << "readbytes =" << readbytes             << ", writebytes =" << writebytes << Endl;    }}

Random Read and write of files

5.lseek

Corresponds to the fseek in the C library function by specifying the number of bytes relative to the current position, end position, or start position to reposition CURRP:

off_t lseek (int fd, off_t offset, int whence);

Return value: The new file offset value;

Whence value:

Seek_set

The offset is set to offset bytes.

Seek_cur

The offset is a set to their current location plus offset bytes.

Seek_end

The offset is set to the size of the file plus offset bytes.

//example 1int main (int argc, char *argv[])    {int FD = open ("Test.txt", o_rdonly);    if (fd = =-1) err_exit ("Open error");    Char buf[1024] = {0};    int readbytes = Read (FD, buf, 5);    cout << "readbytes =" << readbytes << ", buf:" << buf << Endl;    int seekcount = Lseek (FD, 0, seek_cur); cout << "Current offset =" << seekcount << Endl;} 
Example 2: Generating an empty file int main (int argc, char *argv[]) {    int fd = open ("Hole.txt", o_wronly| O_creat| o_trunc| O_EXCL, 0666);    if (fd = =-1)        err_exit ("open Error");    if (Write (FD, "ABCDE", 5) = =-1)        err_exit ("First write error");    Create a 1G file    if (Lseek (FD, 1024*1024*1024, seek_cur) = =-1)        err_exit ("Lseek error");    if (Write (FD, "Hello", 5) = =-1)        err_exit ("Second write error");    Close (FD);}

Attached

-View Hole.txt file

Od-c Hole.txt

cat-a Hole.txt

-View the file size

du-h Hole.txt

du-b Hole.txt

du-k Hole.txt

du-m Hole.txt

Directory Access

6.opendir

       #include <sys/types.h>       #include <dirent.h>       DIR *opendir (const char *name);

return value:

Success: Returns the directory pointer;

Failed: returned null;

7.readdir

struct Dirent *readdir (DIR *dirp);

return value:

Success: Returns a pointer to the dirent structure that contains the details of the next connection for the specified directory;

Returns 0 when there are no more connections;

struct dirent{    ino_t          D_ino;       /* inode number */    off_t          D_off;       /* not an offset; See NOTES */    unsigned short d_reclen;    /* Length of this record */    unsigned char  d_type;      /* type of file; Not supported by all                                         filesystem types */    char           d_name[256];/* filename */};

8.closedir: Close Directory

int Closedir (DIR *dirp);
Example: simple ls program int main (int argc, char *argv[]) {    if (argc < 2)    {        cerr << "Usage:" << argv[0] &l t;< "<directory>" << Endl;        Exit (exit_failure);    }    DIR *dir = Opendir (argv[1]);    if (dir = = NULL)        err_exit ("Opendir error");    struct dirent *ent;    while (ent = Readdir (dir)) = NULL)    {        //filter out hidden files        if (ent->d_name[0] = = '. ')            Continue;        cout << ent->d_name << "\ti-node:" << ent->d_ino             << ", Length:" << ent->d_re Clen << Endl;    }    Closedir (dir);}

9.mkdir

int mkdir (const char *pathname, mode_t mode);

10.rmdir: Delete Empty directory

int rmdir (const char *pathname);

Chmod, Fchmod Change permissions

int chmod (const char *path, mode_t mode); int fchmod (int fd, mode_t mode);

12.chown,fchown change file owner/owning Group

int chown (const char *path, uid_t owner, gid_t Group), int fchown (int fd, uid_t owner, gid_t Group);

File I/O practices (1)--Basic API

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.