C++写文件,直接写入结构体
以前写文件都是写入字符串或者二进制再或者就是一些配置文件,今天介绍一下直接写入结构体,可以在软件参数较多的时候直接进行读写,直接将整个结构体写入和读取,这是MFC的 , 看代码:
//写文件
int readPara(void)
{
CFile myFile;
CFileException ex;
if (myFile.Open("D:\1", CFile::modeRead, &ex))
{
myFile.Read(&s, sizeof(s));
myFile.Close();
return 0;
}
else
{
return -1;
}
}
//读文件
int writePara(void)
{
CFile myFile;
CFileException ex;
if (myFile.Open("D:\1", CFile::modeReadWrite|CFile::modeCreate, &ex))
{
myFile.Write(&s, sizeof(s));
myFile.Close();
return 0;
}
else
{
return -1;
}
}