|
//#include "fileSimpleClass.h"
#pragma once
#include "stdafx.h"
//
#include "Convert.h"
#ifndef _fileSimpleClass_H_
#define _fileSimpleClass_H_
class fileSimpleClass
{
public:
Convert cvt;
//打印函数
void pfvalue_default(const ACHAR* filepath);
//打印文件内容2 函数
void pfvalue2(const ACHAR* filepath);
//写入文件内容到txt文件中
void pfvalueWriteLine(char* line);
};
//-----只引用一次头文件才可以把定义类和实现类功能放在一个文件中
//类成员实现
//-----------------------------
//打印文件内容1 C文件操作函数 OK (和读取的文件内容一样)
void fileSimpleClass::pfvalue_default(const ACHAR* filepath)
{
acutPrintf(_T("\n-----下面是c文件操作函数打开的内容-----\n"));
acutPrintf(filepath);acutPrintf(_T("\n"));
FILE *fp;
int linesize=4000;
char *line;line=new char[linesize];
const char* path=cvt.ConvertAcharPtrToCharPtr(filepath);
ACHAR* wtmp=cvt.ConvertCharPtrToAcharPtr(path);
acutPrintf(wtmp);acutPrintf(_T("\n"));
if((fp=fopen(path,"r"))==NULL)
{
acutPrintf(_T("\nfile cannot be opened\n"));
}
else
{
const ACHAR* tmp;
while(!feof(fp))
{
if(fgets(line,linesize,fp)!=NULL)
{
tmp=cvt.ConvertCharPtrToAcharPtr(line);
acutPrintf(tmp);
pfvalueWriteLine(line);
}
}
fclose(fp);
}
}
//打印文件内容2 ifstream类 OK (空行自动去掉)
void fileSimpleClass::pfvalue2(const ACHAR* filepath)
{
//--file2 open
//filepath="d:\\test.txt";
acutPrintf(ACRX_T("\n"));
acutPrintf(filepath);
ifstream r(filepath,ifstream.in);
if(!r)
{
acutPrintf(ACRX_T("打开文件出错!"));
}
else
{
const ACHAR* tmpAchar;
//char line[4000]; //[100]2000
int linesize=4000;
char* line;line=new char[linesize];
while(r>>line)
{
tmpAchar=cvt.ConvertCharPtrToAcharPtr(line);
acutPrintf(tmpAchar);
acutPrintf(ACRX_T("\n"));
}
r.close();
acutPrintf(ACRX_T("输出文件内容完毕!"));
}
};
void fileSimpleClass::pfvalueWriteLine(char* line)
{
char* fpath="d:\\testwrite.txt";
ofstream w(fpath,ofstream.app);
if(!w)
{
acutPrintf(ACRX_T("打开文件出错!"));
}
else
{
w.write(line,strlen(line));
w.flush();
w.close();
}
};
#endif |
|