In the C-write program, often need to store some simple data, if this is the use of MySQL database is small, you can write the data to the structure of the file, and then need to read the file, take out the data.
The following are the source and header files that define the function:
Source file struct.c:
#include "Struct.h"
The first parameter is the file name to write, the second parameter is the buffer, and the third parameter is the buffer size.
The fourth parameter is the form that opens the file stream, and returns true to write success, and returns false to write failure
int writestruct (const char *filename,char *buffer,int Bufferlen,char *mode) {
int ret;
FILE *fileid = NULL;
Fileid = fopen (Filename,mode);
if (Fileid = = NULL) {
Perror ("fopen");
Goto Writeend;
}
Rewind (Fileid);
ret = fwrite (Buffer,bufferlen,1,fileid);
if (ret <= 0) {
Perror ("fwrite");
Goto Writeend;
}
if (Fileid!= NULL) {
Fclose (Fileid);
Fileid = NULL;
}
return TRUE;
Writeend:
if (Fileid!= NULL) {
Fclose (Fileid);
Fileid = NULL;
}
return FALSE;
}
The first parameter is the file name to read, the second parameter is the buffer, the third parameter is the buffer size, the fourth parameter is the form of the file flow open, and the return TRUE indicates the read success, and return false to read failed
int readstruct (const char *filename,char *buffer,int Bufferlen,char *mode) {
int ret;
FILE *fileid = NULL;
Fileid = fopen (Filename,mode);
if (Fileid = = NULL) {
Perror ("fopen");
Goto Readend;
}
Rewind (Fileid);
memset (buffer,0,sizeof (buffer));
ret = fread (Buffer,bufferlen,1,fileid);
if (ret >= 0) {
strcat (Buffer, "");
}else{
Perror ("Fread");
Goto Readend;
}
if (Fileid!= NULL) {
Fclose (Fileid);
Fileid = NULL;
}
return TRUE;
Readend:
if (Fileid!= NULL) {
Fclose (Fileid);
Fileid = NULL;
}
return FALSE;
}