#include "struct.h"
//第一个参数是要写入的文件名,第二个参数是缓冲区,第三个参数是缓冲区大小,
第四个参数是打开文件流的形态,返回TRUE表示写入成功,返回FALSE表示写入失败
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;
}
//第一个参数是要读取的文件名,第二个参数是缓冲区,第三个参数是缓冲区大小,
第四个参数是打开文件流的形态,返回TRUE表示读取成功,返回FALSE表示读取失败
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,"\0");
}else{
perror("fread") ;
goto readEnd;
}
if (fileID != NULL){
fclose(fileID);
fileID = NULL;
}
return TRUE;
readEnd:
if (fileID != NULL){
fclose(fileID);
fileID = NULL;
}
return FALSE;
} |