📄 filemanager.cpp
字号:
#include "filemanager.h"
#include <io.h>
#include <FCNTL.H>
#include <memory.h>
#include <string.h>
FileManager::FileManager()
{
this->content=NULL;
}
FileManager::~FileManager()
{
if(this->content)
delete []this->content;
}
/*****************************************************
从指定文件读取一行
*****************************************************/
void FileManager::ReadLine(FILE *fp, char *buf)
{
if(fgets(buf,MAX_LINE_CHAR,fp)==NULL)return;
for(int i=0;i<MAX_LINE_CHAR;i++)
{
if(buf[i]==0x0a || buf[i]==0x0d)
{
buf[i]='\0';
break;
}
}
}
/*****************************************************
以空格或制表符为间隔解析一个数据
*****************************************************/
int FileManager::ParseData(int pos,const char *buf,char *szRes)
{
int i=pos,j=0;
while((buf[i]==32 || buf[i]==9 || buf[i]==',') && buf[i])i++;
while(!(buf[i]==32 || buf[i]==9 || buf[i]==',') && buf[i])
{
szRes[j]=buf[i];
i++;
j++;
}
szRes[j]=0;
return i;
}
/*****************************************************
读入文件的所有内容,文件超过指定尺寸
*****************************************************/
void FileManager::LoadFileContent(const char *file_name)
{
int buf_len;
int fh=_open(file_name, _O_RDWR);
if(fh!=-1)
{
buf_len=_filelength(fh)+1;
_close(fh);
}
else
buf_len=MAX_CONTENT_CHAR;
this->content=new char[buf_len];
memset(this->content,0,buf_len);
FILE *fp=fopen(file_name,"r");
if(fp==NULL)
return;
fread(this->content, sizeof(char), buf_len-1,fp);
fclose(fp);
}
/*****************************************************
释放载入的文件内容
*****************************************************/
void FileManager::ReleaseFileContent()
{
if(this->content)
delete []this->content;
this->content=NULL;
}
/*****************************************************
解析读取设置内容的一个项
*****************************************************/
char *FileManager::ParseItemValue(const char *ItemName)
{
if(!this->content || !ItemName)return NULL;
char *str=strstr(this->content,ItemName);
int i;
char *res;
if(!str)return NULL;
str=strstr(str,"=");
for(i=1; str[i]; i++)
{
if(str[i]==0x0a)
{
i--;
break;
}
}
res=new char[i+1];
memcpy(res,str+1,sizeof(char)*i);
res[i]=0;
return res;
}
/*****************************************************
读入训练或者测试数据
*****************************************************/
/*double *FileManager::ReadData(int number_of_inputs,int &number_of_data_points,char *file_name)
{
char buf[MAX_LINE_CHAR];
char str[100];
FILE *fp = fopen(file_name,"r");
double *res;
int i,j,p;
int nPos;
this->ReadLine(fp,buf);
number_of_data_points = atoi(buf);
res = new double[number_of_data_points*(number_of_inputs+1)];
for(i = 0, p = 0; i < number_of_data_points; i++)
{
this->ReadLine(fp,buf);
nPos=0;
for(j=0;j<number_of_inputs+1;j++,p++)
{
nPos=this->ParseData(nPos,buf,str);
res[p]=atof(str);
}
}
fclose(fp);
return res;
}*/
double *FileManager::ReadData(int number_of_inputs,int &number_of_data_points, char *file_name)
{
int i,j;
FILE *fp;
char buf[MAX_LINE_CHAR];
double *res=NULL;
fp = fopen(file_name, "r");
this->ReadLine(fp,buf);
number_of_data_points = atoi(buf);
res = new double[number_of_data_points*(number_of_inputs+1)];
//res=(double *)malloc(number_of_data_points*(number_of_inputs+1)*sizeof(double));
if(!res)
return NULL;
memset(res, 0, number_of_data_points*(number_of_inputs+1)*sizeof(double));
for(i=0; i<number_of_data_points;i++)
{
for(j=0; j<number_of_inputs+1;j++)
{
fscanf(fp, "%lf", &res[i*(number_of_inputs+1)+j]);
}
}
fclose(fp);
return res;
}
/*****************************************************
在指定文件中追加入一行
*****************************************************/
void FileManager::WriteFileLine(const char *str,char *file_name)
{
FILE *fp=fopen(file_name,"a");
if(!fp)return;
fprintf(fp,"%s\n",str);
fclose(fp);
}
/*****************************************************
读取交叉验证训练数据
*****************************************************/
double *FileManager::ReadCrossTrnData(char *dataset_name, int set_id, int number_crossset, int number_of_inputs, int &number_of_data_points)
{
char str[255];
int i,j,k,ch;
int *count=(int *)malloc(sizeof(int)*number_crossset);
int number_sample=0;
for(i=0;i<number_crossset;i++)
{
if(i==set_id)continue;
sprintf(str,"./dat/%s/%s_%d.txt",dataset_name,dataset_name,i);
FILE *fp=fopen(str,"r");
if(!fp)
{
free(count);
return NULL;
}
count[i]=0;
while((ch=fgetc(fp))!=EOF)
if(ch=='\n')
count[i]++;
fclose(fp);
number_sample+=count[i];
}
number_of_data_points=number_sample;
//AllocData(trn_data);
//double *res=(double *)malloc(number_sample*(number_of_inputs+1)*sizeof(double));
double *res=new double[number_sample*(number_of_inputs+1)*sizeof(double)];
double *p=res;
for(i=0;i<number_crossset;i++)
{
if(i==set_id)continue;
sprintf(str,"./dat/%s/%s_%d.txt",dataset_name,dataset_name,i);
FILE *fp=fopen(str,"r");
for(j=0;j<count[i];j++)
{
for(k=0;k<number_of_inputs+1;k++, p++)
fscanf(fp,"%lf", p);
}
fclose(fp);
}
free(count);
return res;
}
/*****************************************************
读取交叉验证测试数据
*****************************************************/
double *FileManager::ReadCrossTstData(char *dataset_name, int set_id, int number_crossset, int number_of_inputs, int &number_of_data_points)
{
char str[255];
int j,k,ch;
int number_sample=0;
sprintf(str,"./dat/%s/%s_%d.txt",dataset_name,dataset_name,set_id);
FILE *fp=fopen(str,"r");
while((ch=fgetc(fp))!=EOF)
if(ch=='\n')
number_sample++;
fclose(fp);
number_of_data_points=number_sample;
//double *res=(double *)malloc(number_sample*(number_of_inputs+1)*sizeof(double));
double *res=new double[number_sample*(number_of_inputs+1)*sizeof(double)];
double *p=res;
sprintf(str,"./dat/%s/%s_%d.txt",dataset_name,dataset_name,set_id);
fp=fopen(str,"r");
for(j=0;j<number_sample;j++)
{
for(k=0;k<number_of_inputs+1;k++, p++)
fscanf(fp,"%lf", p);
}
fclose(fp);
return res;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -