文件及文件操作
#include <iostream.h>class File;
class FileRef
{
public:
class File& m_file;
unsigned long m_nIndex;
char m_c;
public:
FileRef(File& fp,unsigned long nIndex)
:m_file(fp),m_nIndex(nIndex)
{
}
FileRef& operator=(char c);
operator char ();
};
class File
{
friend class FileRef;
public:
FILE * m_fp;
public:
File(const char * szFileName)
{
m_fp = fopen(szFileName,"w+");
}
~File()
{
if (m_fp)
{
fclose(m_fp);
m_fp = NULL;
}
}
FileRef operator[](unsigned long nIndex)
{
return FileRef(*this,nIndex);
}
};
FileRef& FileRef::operator=(char c)
{
fseek(m_file.m_fp,m_nIndex,SEEK_CUR );
fwrite(&c,sizeof(char),1,m_file.m_fp);
return *this;
}
FileRef::operator char()
{
fseek(m_file.m_fp,m_nIndex,0);
fread(&m_c,sizeof(char),1,m_file.m_fp);
return m_c;
}
int main(int argc, char* argv[])
{
File fp("temp.txt");
fp = 'A';
char cc = fp;
cout<< cc << endl;
return 0;
} 源于课件的一段代码 太恶了 ~~
页:
[1]