There is a lot to learn today:
I plan to study more today. If I study too little, it will take too much time. I am going to hurry up.
=======================================================
Additions to file creation and opening:
O_EXCL:
When the file already exists, the opening fails; then we write the code like this:
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main() { int fd; fd=open("./file1",O_RDWR|O_CREAT|O_EXCL,0600); if(fd==-1) { printf("file1 has created!\n"); //File exists and failed to open, fd=-1 } if(fd > 0) { printf("file1 creat success\n"); } return 0; }
Let's run it to see if it's correct.
After deleting, we run ./a.out to create a new file1 successfully. When file1 exists, let’s run it again:
At this time, the file has been created!
O_APPEND:
Before we understand this, let's take a look at the following codes: This is the writing of the previously used files
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <string.h> #include <stdlib.h> int main() { int fd; char *buf = "Hello Wrold!Linux"; fd=open("./file1",O_RDWR); printf("open success : fd=%d",fd); write(fd,buf,strlen(buf)); return 0; }
when we write something in the text
Let's run ./a.out again to see the phenomenon
However, we also need to know a small phenomenon, that is, it is overwritten from the beginning. If the original text content has more bytes than the newly overwritten content, the new content will only overwrite the original part corresponding to its own byte count. , for example:
Originally 12345678
What I want to override is abcde
Then what we finally get is: abcde678
The things written before have been overwritten. How to solve the problem of not overwriting and writing things? We need to use it:
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <string.h> #include <stdlib.h> int main() { int fd; char *buf = "Hello Wrold!Linux"; fd=open("./file1",O_RDWR|O_APPEND); printf("open success : fd=%d",fd); write(fd,buf,strlen(buf)); return 0; }
Let's run it and see what happens
It can be found that the text content is not covered, and the new text content is written in another line
This function reminds us of the Pokémon games we have played before. We can create a new save file, or overwrite a save file. The general principle should be the same, hahahaha!
O_TRUNC:
This is similar to the above. It completely discards all the original text content and writes new text content. It can be regarded as a new rewrite:
Our code is as follows, let's test it out:
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <string.h> #include <stdlib.h> int main() { int fd; char *buf = "Hello Wrold!Linux"; fd=open("./file1",O_RDWR|O_TRUNC); printf("open success : fd=%d",fd); write(fd,buf,strlen(buf)); return 0; }
Let's start writing such a long text, let's run our code and try:The result is obvious, it has been completely covered.
Create file creat function
The old rule is to use the man command to view the manual and take a look at the usage guide:
We write code like this:
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <string.h> #include <stdlib.h> int main() { int fd; char *buf = "Hello Wrold!Linux"; fd= creat("/home/tang/desktop/file1",S_IRWXU); //s_irwxu can read and write execution //int open(const char *pathname, int flags, mode_t mode); }
Let's compile it:
Here you can see that file1 was successfully created, and file1 is a readable, readable and executable file
Finish!