catalogue
1. Write an integer to the file
2. Write a structure to the file
3. Write structure array to file
1. Write an integer to the file
First create an empty file: file
Then the code starts
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <stdlib.h> int main(int argc,char **argv) { int fd; int data = 100; int data2 = 0; fd = open("./file",O_RDWR); int n_write = write(fd,&data,sizeof(int)); lseek(fd,0,SEEK_SET); int n_read = read(fd,&data2,sizeof(int)); printf("read %d \n",data2); close(fd); return 0; }
As a result, the written data can be read out accurately
Enter the file file to view the written data and find similar garbled code. In fact, it's not. It's just asiii code (ask code) translated for machines, not for people to read.
It is successfully written for the program.
2. Write a structure to the file
First create an empty file: file
Then the code starts
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <stdlib.h> struct Test { int a; char c; }; int main(int argc,char **argv) { int fd; struct Test data = {100,'a'}; struct Test data2; fd = open("./file",O_RDWR); int n_write = write(fd,&data,sizeof(struct Test)); lseek(fd,0,SEEK_SET); int n_read = read(fd,&data2,sizeof(struct Test)); printf("read %d,%c \n",data2.a,data2.c); close(fd); return 0; }
As a result, the written data can be read out accurately
Enter the file file to view the written data and find similar garbled code. In fact, it's not. It's just asiii code (ask code) translated for machines, not for people to read.
It is successfully written for the program.
3. Write structure array to file
First create an empty file: file
Then the code starts
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <stdlib.h> struct Test { int a; char c; }; int main(int argc,char **argv) { int fd; struct Test data[2] = {{100,'a'},{101,'b'}}; struct Test data2[2]; fd = open("./file",O_RDWR); int n_write = write(fd,&data,sizeof(struct Test)*2); lseek(fd,0,SEEK_SET); int n_read = read(fd,&data2,sizeof(struct Test)*2); printf("read %d,%c \n",data2[0].a,data2[0].c); printf("read %d,%c \n",data2[1].a,data2[1].c); close(fd); return 0; }
As a result, the written data can be read out accurately
Enter the file file to view the written data and find similar garbled code. In fact, it's not. It's just asiii code (ask code) translated for machines, not for people to read.
It is successfully written for the program.
4. Write linked list to file
The operation of the linked list is the same as the above. The only difference is that when reading the linked list, you can't read it directly. You should traverse the linked list to get the number of linked lists (that is, the size of the linked list), and then traverse the reading of structures one by one. Just fine. For example, if the address of the array is continuous and a block is read, you can read it directly. The linked list is not a block. It needs to be read node by node.
5. Summary
The first four points are to let your mind not be in a fixed position for buffer buf, thinking that only char type can be put in the buffer. No, integers, structures, etc.