/**********************************************
* VC day02
* class :
* CArray(int , int&);
* CArray(TYPE, TYPE&);
* CList(int , int&);
* CList(TYPE, TYPE&);
* CreateFile
* WriteFile
* CloseHandle
* CFile
* description:
* 使用MFC集合类
* CArray 数组类
* CList 数组类
* 新建win32控制台工程,空工程
* “工程”-》“设置”
* -》“常规”-》“MicroSoft基础类”
* -》“使用MFC作为静态连接库”
*
**********************************************/
#include <afx.h>
#include <afxtempl.h>
#include <winbase.h>
#include <windef.h>
#include <string.h>
class Person: public CObject
{
private:
CString name;
CString addr;
public:
Person();
Person(CString a,CString b);
void show();
//virtual void Serialize(CArchive& archive);
void Serialize(CArchive& archive);
DECLARE_SERIAL(Person);
};
IMPLEMENT_SERIAL(Person, CObject, 1);
Person::Person()
{
}
Person::Person(CString a,CString b)
{
name = a;
addr = b;
}
void Person::show()
{
printf("%s,%s/n", name, addr);
}
void Person::Serialize(CArchive& archive)
{
CObject::Serialize(archive);
if(archive.IsStoring())
{
archive << name << addr;
}else{
archive >> name >> addr;
}
}
void array_fun();
void list_fun();
void map_fun();
void win32iowrite();
void win32ioread();
void file_fun();
void filelist_fun();
void archive_fun();
int main()
{
//list_fun();
//map_fun();
//win32iowrite();
//win32ioread();
//file_fun();
//filelist_fun();
archive_fun();
return 0;
}
void array_fun()
{
int a = 12;
int b = 42;
CArray<int,int&> array;
array.Add(a);
array.Add(b);
int i;
for (i = 0;i < array.GetSize();i++)
{
printf("%d/n", array.GetAt(i));
}
}
void list_fun()
{
int a = 3;
int b = 46;
CList<int,int&> list;
list.AddHead(a);
list.AddHead(b);
int i;
POSITION pos = list.GetHeadPosition();
for (i = 0;i < list.GetCount();i++)
{
printf("%d/n", list.GetNext(pos));
}
}
void map_fun()
{
CMapStringToString map;
map.SetAt("aaaa", "bbbb");
map.SetAt("cccc", "dddd");
CString key;
CString value;
map.Lookup("aaaa", value);
printf("%s/n", value);
int i;
POSITION pos = map.GetStartPosition();
for (i = 0;i < map.GetCount();i++)
{
map.GetNextAssoc(pos, key, value);
printf("%s,%s/n", key, value);
}
}
void win32iowrite()
{
HANDLE handle;
handle = CreateFile("hello.txt"
, GENERIC_WRITE
, 0
, NULL
, OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, 0);
CString s("hello");
DWORD lenwirte;
//SetEndOfFile(handle);
SetFilePointer(handle, 0, 0, FILE_END);
WriteFile(handle, s, s.GetLength(), &lenwirte, NULL);
CloseHandle(handle);
}
void win32ioread()
{
HANDLE handle;
handle = CreateFile("hello.txt"
, GENERIC_WRITE|GENERIC_READ
, 0
, NULL
, OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, 0);
CString s("hello");
char content[100];
DWORD lenwirte;
//SetEndOfFile(handle);
//SetFilePointer(handle, 0, 0, FILE_END);
ReadFile(handle, content, 100, &lenwirte, NULL);
content[lenwirte] = '/0';
printf("%s/n", content);
CloseHandle(handle);
}
void file_fun()
{
CFile mfile("CFile.txt", CFile::modeReadWrite | CFile::modeCreate);
CString str("大家好啊!");
mfile.Write(str, str.GetLength());
mfile.SeekToBegin();
char buf[100];
memset(buf, '/0',100);
int buflen = mfile.Read(buf, 100);
buf[buflen] = '/0';
printf("%s/n", buf);
mfile.Close();
}
void filelist_fun()
{
WIN32_FIND_DATA data;
HANDLE handle;
handle = FindFirstFile("*.*", &data);
while(FindNextFile(handle, &data))
{
printf("%s/n", data.cFileName);
}
}
void archive_fun()
{
Person *p = new Person("xiujie", "jinan");
CFile mfile("archive.txt", CFile::modeReadWrite | CFile::modeCreate);
CArchive archive(&mfile, CArchive::store);
archive << p;
//Person *p_load;
//CArchive archive_load(&mfile, CArchive::load);
//archive_load >> p_load;
//p_load->show();
p->show();
mfile.Close();
}