分类: C/C++
2007-11-16 21:25:22
#include "stdafx.h"
#include
#include
#include
using namespace std;
class CMyClass;//申明类
CArchive& AFXAPI operator << (CArchive& ar, const CMyClass& my);//重载串行化运算符
CArchive& AFXAPI operator >> (CArchive& ar, CMyClass& my);
class CMyClass//类定义
{
public:
CMyClass()
{
nx=10;
ny=10;
strName="";
}
BOOL SaveFile(CFile& file);//增加对CFile类的支持
BOOL ReadFile(CFile& file);
BOOL SaveToFile(LPCTSTR lpszFileName);//增加磁盘文件的读写
BOOL ReadFromFile(LPCTSTR lpszFileName);
VOID SaveFont(CString sFileName);
VOID LoadFont(CString sFileName);
VOID Serialize(CArchive& ar);
~CMyClass(){;}
friend CArchive& AFXAPI operator << (CArchive& ar, const CMyClass& my);//申明为友元,以便可以访问到其非公成员
friend CArchive& AFXAPI operator >> (CArchive& ar, CMyClass& my);
private://成员变量
int nx,ny;
CString strName;
CFont m_font;
};
void CMyClass::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{ // storing code
//假如CMyDialog有一成员CString mystring,将mystring 写入文件:
ar<
else
{ // loading code
//读出数据:
ar>>strName;
}
//文件的读写是在Serialize()中完成的
}
VOID CMyClass::SaveFont(CString sFileName){
CFile file(sFileName,CFile::modeCreate|CFile::modeReadWrite);
LOGFONT logFont;
m_font.GetLogFont(&logFont);
file.Write(&logFont,sizeof(LOGFONT));
file.Close();
}
VOID CMyClass::LoadFont(CString sFileName){
CFile file(sFileName,CFile::modeRead);
LOGFONT logFont;
file.Read(&logFont,sizeof(LOGFONT));
file.Close();
m_font.CreateFontIndirect(&logFont);
}
BOOL CMyClass::SaveFile(CFile& file)
{
try
{
CArchive ar(&file,CArchive::store);
ar<<(*this);
//Serialize(ar);
ar.Close();
}
catch(...)
{
return FALSE;
}
return TRUE;
}
BOOL CMyClass::ReadFile(CFile& file)
{
try
{
CString cLine;
file.SeekToBegin();
CArchive ar(&file,CArchive::load);
//Serialize(ar);
//ar.ReadString(cLine);
ar>>(*this);
//ar>>this->strName;
ar.Close();
}
catch(...)
{
return FALSE;
}
return TRUE;
}
CArchive& AFXAPI operator << (CArchive& ar, const CMyClass& my)
{
ASSERT(ar.IsStoring());
ar<
}
CArchive& AFXAPI operator >> (CArchive& ar, CMyClass& my)
{
CString tmpLine;
ASSERT(ar.IsLoading());
ar>>my.nx;
ar>>my.ny;
while (ar.ReadString( tmpLine)){
tmpLine.ReleaseBuffer();
cout << tmpLine << endl;
my.strName += tmpLine;
my.strName += "\r\n";
}
return ar;
}
BOOL CMyClass::SaveToFile(LPCTSTR lpszFileName)
{
BOOL bRe=TRUE;
try
{
CFile file(lpszFileName,CFile::modeCreate | CFile::modeWrite);
if(!SaveFile(file))
{
bRe=FALSE;
}
file.Close();
}
catch(CFileException* e)
{
char cErr[1024];
e->GetErrorMessage(cErr,1024);
cout << cErr << endl;
return FALSE;
}
return TRUE;
}
BOOL CMyClass::ReadFromFile(LPCTSTR lpszFileName)
{
BOOL bRe=TRUE;
try
{
CFile file(lpszFileName,CFile::modeRead);
if(!ReadFile(file))
{
bRe=FALSE;
}
file.Close();
}
catch(CFileException* e)
{
char cErr[1024];
e->GetErrorMessage(cErr,1024);
cout << cErr << endl;
return FALSE;
}
return TRUE;
}
#define D
#ifdef D
int _tmain(int argc, _TCHAR* argv[])
{
CMyClass myCls;
CString cFile("Read.txt");
char cNewFile[32]="tech.txt";
//CFile file(LPCTSTR(cFile),CFile::modeRead);
myCls.ReadFromFile(LPCTSTR(cFile));
myCls.SaveToFile(cNewFile);
return 0;
}
#endif
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void Read3dDataEachLine(const char* szFileName = "test.txt")
{
if(szFileName == NULL)
return;
char FullPath[100] = {"\0"};
::GetModuleFileName(NULL,FullPath,100);
PathRemoveFileSpec(FullPath);
string File = FullPath;
File += "\\";
File += szFileName;
FILE* pFile = fopen(File.c_str(),"r");
if (NULL == pFile)
{
cout<<"读文件出错!"<
}
char szLine[100] = {0};
char cData;
int nIndex = 0;
while (fread(&cData,1,1,pFile) > 0)
{
if (cData == '\n')
{
ProcessEachLine(szLine);//对每行进行处理
strcpy(szLine,"\0");
nIndex = 0;
}
else
{
szLine[nIndex] = cData;
nIndex++;
}
}
}
.......................................................
#include
#include
#include
#include
using namespace std;
//read file
vector
string strtmp;
ifstream infile("123.txt");
while ( infile >> strtmp )
{
s.push_back( strtmp );
}
//find
string sc="11";
vector
for(it = s.begin(); it != s.end(); ++it)
{
string stmp = *it;
int ifind = stmp.find(sc, 0);
if(0 == ifind)
cout << *it << '\n';
}
.................................................
void ReadFromFile(const CString& strFile)
{
if(strFile.IsEmpty())
{
AfxMessageBox(_T("Invaild Path!"));
return;
}
CStdioFile file;
if(file.Open(strFile,CFile::modeRead|CFile::typeText ) )
{
CString str = _T("");
while (file.ReadString(str))
{
m_vector.push_back(str);//m_vector为vector
}
}
}