The creation and application of static library and dynamic library in C language program

Source: Internet
Author: User
Tags fread goto rewind mysql database

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.