0723------Linux Base----------file io File Open

Source: Internet
Author: User

1. Opening of the file

1.1 Open and fopen. Open Returns a file descriptor, and fopen returns a pointer to the file, the second parameter is also different, one is the macro definition, and the other is a string. Therefore, you should pay special attention when writing.

  int fd = open ("Test.txt", o_rdonly);
  FILE *FP = fopen ("Test.txt", "R");

  1.2 Conversion between FILE * and FD

   1.2.1 From File * into FD. The Fileno function is used to convert a file pointer to a file descriptor, and a Linux function can be used at this point.

FILE *FP = fopen ("Test.txt", "R"); int fd = Fileno (FP);

1.2.2 converted from FD to FILE *.     the fdopen function is used to convert a file descriptor into a pointer, and a standard file function can be used at this time. The open mode of the FP file pointer here coincides with the open mode of the FD file descriptor, i.e. read-only is read-only.

int fd = open ("Test.txt", o_rdonly); FILE *FP = Fdopen (FD, "R");

1.3  Common open flag combinations:

A) O_rdonly: represents read-only

b) o_wronly | O_creat: Opened as write-only, the file does not exist and is created;

c) o_wronly | O_creat | O_append: Write-only open, the file does not exist is created, the file exists is appended;

d) o_wronly | O_creat | O_trunc: Write-only open, do not exist then create, file exists is empty;

e) o_wronly | O_creat | O_EXCL: Write-only open, do not exist, create, file exists then open failed.

1.4 File pointer stdin, Stdout,stderr inside must be called Stdin_fileno, Stdout_fileno, Stderr_fileno these three file descriptors (because the Linux underlying is implemented with file descriptors).

2. Process open a file descriptor involving the data structure

2.1 The Linux process opens a file descriptor that involves three data structures:

A) the process table entry , which is owned by a single process , records all file descriptors opened by the process.

b) file table entries , managed by the kernel, can correspond to an open file operation, which records the open file's flag , file offset , and pointer to the file, and the reference count for the structure (how many fd points to it at this point).

c) v node entry , each of which corresponds to a file that records information about the file (size, creation time, modification time, owner, permissions).

  2.2 Different scenarios for three data structures when opening a file:

   A) a process opens two different files , process table entry 1, File Table entry 2, v Node 2;

b) a process two times open the same file , process table entry 1, File Table entry 2 (the two do not share the file offset, so read and write does not interfere), v node 1;

c) Two processes open the same file , process table entry 2, File Table entry 2, v node 1;

D) a process opens a file and then uses the DUP to copy a file descriptor, at this time, the process table entry 1, the File Table entry 1 (where the reference count is 2, which is the same as fork a child process), v Node 1.

3.lseek function

, the current file offset attribute exists in the file table entry, Each open file describes the nonalphanumeric associated with a current file offset. This example is used to return the value of the current offset.

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include < sys/stat.h> #include <fcntl.h> #define ERR_EXIT (m) do     {         perror (m);        Exit (exit_failure);    } while (0)/* * Lseek function * */int main (int argc, const char *argv[]) {    int fd = open ("Test.txt", o_rdonly);    if (fd = =-1) {        err_exit ("open");    }    Char buf[1024] = {0};    Read (FD, buf, 5);    printf ("BUF:%s\n", buf);    Sets the offset of the file that exists in the File Table entry    //returns the current offset in bytes from the file header//    here is equivalent to getting the current file offset    off_t len = lseek (fd, 0, seek_cur);    printf ("offset =%d\n", (int) len);    return 0;}

3.2 files are typically read and write operations that begin with the current file offset. in this example, the offset of the open file is set to a value of 6 bytes, then read the next time from the beginning of the current file offset (6 in this case).

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include < sys/stat.h> #include <fcntl.h> #define ERR_EXIT (m) do     {         perror (m);        Exit (exit_failure);    } while (0)/* * Lseek function * */int main (int argc, const char *argv[]) {    int fd = open ("Test.txt", o_rdonly);    if (fd = =-1) {        err_exit ("open");    }    off_t len = Lseek (FD, 6, seek_cur); Sets the current offset to the current offset of +6    printf ("offset =%d\n", (int) len);    Char buf[1024] = {0};    Read (FD, buf, 5);    printf ("BUF:%s\n", buf);    Len = Lseek (fd, 0, seek_cur);    printf ("offset =%d\n", (int) len);    return 0;}

3.3 Lseek can also get the size of the file . Here od-c filename to view the contents of the file can see the size of the file, note that the left is 8 binary.

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include < sys/stat.h> #include <fcntl.h> #define ERR_EXIT (m) do     {         perror (m);        Exit (exit_failure);    } while (0)/* * Lseek function * Gets the size of the file */int main (int argc, const char *argv[]) {    int fd = open ("Test.txt", o_rdonly);    if (fd = =-1) {        err_exit ("open");    }    off_t len = lseek (fd, 0, seek_end);//Set offset to Seek_end    printf ("offset =%d\n", (int) len);    return 0;}

  

3.4 Lseek When setting the file offset, write 0 if no data is currently present.

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include < sys/stat.h> #include <fcntl.h> #define ERR_EXIT (m) do     {         perror (m);        Exit (exit_failure);    } while (0)/* */int main (int argc, const char *argv[]) {    int fd = open ("Test.txt", O_wronly | O_creat | O_TRUNC);    if (fd = =-1) {        err_exit ("open");    }    Write (FD, "Hello", strlen ("Hello"));    off_t len = Lseek (FD, ten, seek_cur);    printf ("offset =%d\n", (int) len);    Write (FD, "World", Strlen ("World"));    Close (FD);    return 0;}

  

 3.5 File Hole

3.5.1 What is a file hole? When the file offset exceeds the file size, a file hole is created, which is recorded in the file information, but does not occupy too much disk space on the disk. For example, we offset the size of 1G in the file, then the 1G hole is recorded in the file information, but the disk does not occupy the actual space, as shown in.

3.5.2 The program sample and the file information and disk information after the program is run.

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/types.h> #include < sys/stat.h> #include <fcntl.h> #define ERR_EXIT (m) do     {         perror (m);        Exit (exit_failure);    } while (0)/* * File Hole * */int main (int argc, const char *argv[]) {    int fd = open ("Test.txt", O_wronly | O_creat | O_TRUNC);    if (fd = =-1) {        err_exit ("open");    }    Char buf[] = "Hello";    Write (FD, buf, strlen (BUF));    off_t len = Lseek (fd, 1024*1024*1024, seek_cur);    printf ("Off_set =%d\n", (int) len);    Write (FD, "World", Strlen ("World"));    Close (FD);

 

  

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.