First attach a file read and write operation: (Today, the head is dizzy, fast into a pig brain ^_^) Write functions, #include <unistd.h> ssize_t write (int fd,const void *buf,size_t nbytes)); Return value: Successfully returns the number of bytes written to the file, failure returns-1 The Write function writes nbytes byte data to the Filedes file, and the data source is BUF.
ssize_t read (int fd,const void *buf,size_t nbytes); Return value: Successfully returns the number of bytes written, and 0 encounters EOF. Failure return-1 The read function reads Nbytes byte data from the Filedes file and stores it in BUF.
#include <fcntl.h> int open (const char *pathname,int oflag,mode_t mode) Return file handle The first parameter is the pathname of the file to be opened/created, and Oflag is used to specify the open/create mode of the file, which can be logically constituted by the following constants. O_rdonly Read Only mode Ycwlinux Federation O_wronly only write mode Ycwlinux Federation O_RDWR reading and writing mode Ycwlinux Alliance When you open/create a file, you must use at least one of the three above to match the following: O_append writes each write to the end of the file Ycwlinux Federation O_creat This file is created if the specified file does not exist Ycwlinux Federation O_EXCL If the file you want to create already exists, return-1 and modify the value of errno Ycwlinux Federation O_trunc if the file exists and is opened in write/read-only mode, empty the entire contents of the File Ycwlinux Federation O_noctty if the path name points to a terminal device, do not use it as a control terminal. Ycwlinux Alliance O_nonblock if the path name points to a fifo/block file/character file, then the file is opened and subsequent I/oycwlinux Federation Set to Non-blocking (nonblocking mode)
#include <stdio.h> #include <stdlib.h> #include <fcntl.h> #define N 10 typedef struct NODE { Char name[20]; struct node *next; }stud; To write a linked list to a file Stud * Create (int n) { Stud *p,*h,*s; int i; H=null; Often forget to define here, causing the following to execute segment fault!! for (i=0;i<n;i++) { if ((s= (Stud *) malloc (sizeof (stud))) ==null) { printf ("malloc space failed!\n"); Exit (0);
} if (h==null) H=s; Else p->next=s; printf ("Please enter Name:"); scanf ("%s", s->name); P=s; } s->next=null; return h; } void print (STUD * head) { while (head) { printf ("name:%s\n", head->name); head=head->next; } } Read a list from a file Stud * Read_userman () { int FD,USERSIZE,RC; Stud *P1,*P2; Stud temp; Static stud head; Usersize=sizeof (stud); Fd=open ("/tmp/user.txt", o_creat| O_RDONLY); if (fd<0) Return Rc=read (fd,&temp,usersize); p1=&head; while (rc>0) { p1->next= (Stud *) malloc (usersize); memcpy (p1->next,&temp,usersize); p1=p1->next; p1->next=null; memset (&temp,0,usersize); Rc=read (fd,&temp,usersize); } Close (FD); if (P1==&head) return NULL; return head.next; } int main () { Stud *head; Head=create (N); Print (head); Write_username (head); Head=read_userman (); Print (head); return 0; } When the linked list is read frequently, it can cause program errors. The following procedure for a delete n data, the original approach to delete a write file, delete a write file, frequent operation, will cause the process of death. So the procedure is to delete n data once, all in memory, until all the data is deleted, then write back to the file. This will not cause the death of the process. int Delete_sslclient_userman_memonry () { struct Sslclient_userman First_userman,*userman,*pre_userman; FILE *FP; Char usernum[10]; int id; Userman=read_sslclient_userman_conf (); First_userman.next=userman; pre_userman=&first_userman; Fp=fopen ("/tmp/user.txt", "R"); while (1) { if (fgets (USERNUM,10,FP) ==null) break; Id=atoi (usernum); while (userman->user_num!=id) && (userman->next!= NULL) { pre_userman=userman; userman=userman->next } if (userman->next==null) { pre_userman->next=null; & nbsp; write_sslclient_userman_conf (First_userman.next); return 1; } Else { pre_userman->next=userman->next; } Userman=first_userman.next; } Write_sslclient_userman_conf (first_userman.next); return 0; } |