File read and write operations to achieve the Linux environment

---- share with you today in linux system to read and write files in the realization of some of the basic operations, before that we have to have some basic skills in the Linux environment. For example, to view the commands and functions of a specific use, which is equivalent to check the manual, there is a man page is very useful in Linux:
man bookset
man 1 + 1 command here represented as a query is Linux command
man 2 2 represented here xxx the query API is Linux
man xxx 3 3 where the query is expressed as c library functions
in the understanding of this we can begin to realize the operation of the said title.
First, in the linux environment common file interface functions: open, close, write, read , lseek.
Second, the basic file operations are divided into steps:
A, linux system to operate in a file, normally open to open a file, to obtain a scanned document descriptor, then the file read and write operations (or other operations), and finally close the file.
b, when the file operation, be sure to open the file, and then perform file operations (open file is unsuccessful, then it can not operate), after the last file operation is completed, be sure to close the file, otherwise it may cause file corruption
c , file usually is stored in a block device file system, we put this file is called static files, when we went to open a file, linux kernel to do operations include: kernel establish a data structure open files in the process ,
the next record we opened this file, apply some kernel memory in the memory, and the contents of static files are read from block device to a specific address in memory for storage management (called dynamic file)
after d, open the file after this file read and write operations, it is in this for the memory of a dynamic document, rather than static files.
When we read and write to files dynamically, at this time the dynamic memory device file and block sync is not a static document,
When we close off dynamic file, close the file inside the kernel memory contents of the dynamic of static files to update the device blocks (synchronous).
Third, why is this operation?
In block write apparatus itself limit (memories Nandflash, SD, and other feature block write device), the operation itself is very inflexible block device. And the memory can be operated by bytes. And random operation.
Fourth, what file descriptor?
1, file descriptors: it is in fact the essence of a number, this number represents a particular meaning in a process, when we open a file open, operating system built some of the data structure in memory to represent the dynamic file, then returned to the application as a digital file descriptor, this figure memory and we maintain this dynamic file linking these data structures on the binding. After we operate this application if you want a dynamic file, just use the file descriptor to distinguish. In simple terms, it is to distinguish between multiple files (when multiple files are open).
2, the scope of the current process is described in the document, out of the current process, the file descriptor does not make sense.
V. code to achieve:
1, open the file:

. 1 #include <stdio.h>
  2 #include <SYS / types.h>
  . 3 #include <SYS / defined in stat.h>
  . 4 #include <fcntl.h>
  . 5 #include <unistd.h> 
 . 6 int main ( int argc , char * the argv [])
  . 7 {
  . 8   // Step: open the file 
 . 9   int fd = - . 1 ;     // fd is the file descriptor (fd file descriptor in the linux 
10   legal range is 0 or is a positive, it can not be negative)
 . 11   FD = Open ( " a.txt " , O_RDWR); // O_RDWR represents a file read and write, this can be 
12  See inside the open function with the use of man has its manual instructions
 13 is   IF (- . 1 == FD) or (FD < 0 )
 14   {
 15     the printf ( " file open error \ n- " );
 16   }
 . 17   the else 
18 is   {
 . 19     the printf ( " file open success \ n- " );
 20 is   }
 21 is  // read file 
22  // close file 
23 is   Close (FD); // close file just opened 
24   return  0 ;                                                   
 25 }

2, read file operations:

 1#include <stdio.h>
 2#include <sys/types.h>
 3#include <sys/stat.h>
 4#include <fcntl.h>
 5#include <unistd.h>
 6int main(int argc,char *argv[])
 7{
 8   int fd =-1;
 9   int ret=-1;
10   char buf[100]={0};
11   fd=open("a.txt",O_RDWR);
12   if(-1==fd)
13   {
14      printf("the open the file is failure \n");
15   }
16   else
17   {
18      printf("the open the file is successful,fd=%d.\n",fd);
19   }
20  ret=read(fd,buf,20);//20表示读取的字节
21
22  if(ret<-1)
23  {
24    printf("read失败\n");
25  }
26  the else 
27   {
 28      the printf ( " successfully read% d bytes \ n- " , RET);
 29      the printf ( " contents of the documents: [% S] \ n- " , buf);
 30   }
 31 is   Close (FD);
 32   return  0 ;                                               
 33 is }

Code compilation effect:
root @ Ubuntu-Virtual-Machine: / mnt / hgfs / Day # gcc file1.c
root @ Ubuntu-Virtual-Machine: / mnt / hgfs / Day # ./a.out
at The Open at The File IS successful, . fd = 3
successfully read 13 bytes of
the file content is: [the Hello world!
]
3, write the file:

 1   #include <stdio.h>
 2   #include <sys/types.h>
 3   #include <sys/stat.h>
 4   #include <fcntl.h>
 5   #include <unistd.h>
 6   #include <string.h>
 7int main(int argc,char *argv[])
 8{ 
 9int fd =-1;
10char buf[100]={0};
11char w_buf[20]="i like the mcu";
12 int K = - 1 ;
13fd=open("a.txt",O_RDWR);
14if(-1==fd)
15{
16  printf("the open the file is failure \n");
17}
18else
19{
20  printf("the open the file is successful,fd=%d.\n",fd);
21}
22  ret=write(fd,w_buf,strlen(w_buf));
23 if(ret<0)
24 {
25   printf("the write is failure\n");
26 }
27 the else 
28 {
 29    the printf ( " Write successful written characters% d \ n- " , RET);
 30 }
 31 is   RET = Read (FD, buf, 20 is ); // 20 is represented by bytes read 
32 IF ( RET <- . 1 )
 33 is {
 34 is   the printf ( " Read failed \ n- " );
 35 }
36 the else 
37 [ {
 38 is    the printf ( " successfully read% d bytes \ n- " , RET);
 39    the printf ( " contents of the documents: [% S] \ n- " , buf);
 40 }
 41 is    Close (FD);
 42 is    return  0 ;                                               
 43 is }

Compile the results:
root @ Ubuntu-Virtual-Machine: / mnt / hgfs / Day # ./a.out
at The Open at The File IS successful, fd = 3.
The Write successful written 14 characters
successfully read a 0 byte
file content is: []
the root @ Ubuntu-Virtual-Machine: / mnt / hgfs / Day a.txt CAT #
I @ Ubuntu-like mcuroot The Virtual-Machine: / mnt / hgfs / Day # Vim file1.c
there is here a defects file is written, when the reading is not read out, this knowledge will continue to learn in the back, do not worry.
VI Summary:
operation of the document, a step to know its operation:
1, open the file
2, read and write files
3, close the file

Guess you like

Origin www.cnblogs.com/1121518wo/p/11616664.html