ios::binary和ios::text打开文件区别,fstream读写文件示例
2010-05-31 17:26
751 查看
把网上搜索的内容大致汇总一下:
1.text方式写入文件会将换行(/n)扩展成/r/n, 读文件时则自动转换回来
2.binary方式则不会作任何扩展,与读写内容一致
3.默认为text方式
详细参考:http://blog.csdn.net/wanfustudio/archive/2007/09/14/1785131.aspx
附录:自己写的使用fstream读写文件的两个函数
#include <fstream>
using namespace std;
bool Preprocess::ReadParseRsltFile(tstring FileName)
{
TCHAR szFull[MAX_PATH] = {0};
_tcscpy_s(szFull, MAX_PATH, m_szDataFileDir);
_tcscat(szFull, TEXT("//"));
_tcscat(szFull, FileName.c_str());
_tcscat(szFull, TEXT("_r"));
wifstream fin;
fin.open(szFull, ios::binary);
if (fin.bad())
{
return false;
}
fin.imbue(std::locale("chs"));
TCHAR szSize[16] = {0};
fin.getline(szSize,16); //第一行为文件大小
int nSize = _ttoi(szSize);
tstring strTmp;
for(int i=0; i<nSize; i++)
{
getline(fin, strTmp);
m_vSenParRslt.push_back(strTmp);
}
fin.close();
return true;
}
void Preprocess::WriteFiles(tstring FileName, const vector<tstring>&vContent)
{
TCHAR szFull[MAX_PATH] = {0};
_tcscpy_s(szFull, MAX_PATH, m_szDataFileDir);
_tcscat(szFull, TEXT("//"));
_tcscat(szFull, FileName.c_str());
wofstream fout;
fout.open(szFull, ios::binary|ios_base::trunc);
// fout.clear();
fout.imbue(std::locale("chs"));
fout<<vContent.size()<<"/n";
for(int i=0; i<vContent.size(); i++)
{
fout<<vContent[i].c_str()<<"/n";
}
fout.close();
}
写入的内容包含中文,所以必须加上: fout.imbue(std::locale("chs"));
tstring 是自己定义的宏,代表string 或者wstring
#ifdef _UNICODE
#define tstring wstring
#else
#define tstring string
#endif
1.text方式写入文件会将换行(/n)扩展成/r/n, 读文件时则自动转换回来
2.binary方式则不会作任何扩展,与读写内容一致
3.默认为text方式
详细参考:http://blog.csdn.net/wanfustudio/archive/2007/09/14/1785131.aspx
附录:自己写的使用fstream读写文件的两个函数
#include <fstream>
using namespace std;
bool Preprocess::ReadParseRsltFile(tstring FileName)
{
TCHAR szFull[MAX_PATH] = {0};
_tcscpy_s(szFull, MAX_PATH, m_szDataFileDir);
_tcscat(szFull, TEXT("//"));
_tcscat(szFull, FileName.c_str());
_tcscat(szFull, TEXT("_r"));
wifstream fin;
fin.open(szFull, ios::binary);
if (fin.bad())
{
return false;
}
fin.imbue(std::locale("chs"));
TCHAR szSize[16] = {0};
fin.getline(szSize,16); //第一行为文件大小
int nSize = _ttoi(szSize);
tstring strTmp;
for(int i=0; i<nSize; i++)
{
getline(fin, strTmp);
m_vSenParRslt.push_back(strTmp);
}
fin.close();
return true;
}
void Preprocess::WriteFiles(tstring FileName, const vector<tstring>&vContent)
{
TCHAR szFull[MAX_PATH] = {0};
_tcscpy_s(szFull, MAX_PATH, m_szDataFileDir);
_tcscat(szFull, TEXT("//"));
_tcscat(szFull, FileName.c_str());
wofstream fout;
fout.open(szFull, ios::binary|ios_base::trunc);
// fout.clear();
fout.imbue(std::locale("chs"));
fout<<vContent.size()<<"/n";
for(int i=0; i<vContent.size(); i++)
{
fout<<vContent[i].c_str()<<"/n";
}
fout.close();
}
写入的内容包含中文,所以必须加上: fout.imbue(std::locale("chs"));
tstring 是自己定义的宏,代表string 或者wstring
#ifdef _UNICODE
#define tstring wstring
#else
#define tstring string
#endif
相关文章推荐
- binary和text方式打开文件的区别
- binary和text方式打开文件的区别(转)
- binary和text方式打开文件的区别
- binary和text方式打开文件的区别
- binary和text方式打开文件的区别(转)
- 文件操作ofstream,open,close,ifstream,fin,按照行来读取数据, fstream,iosin iosout,fio.seekg(),文件写入和文件读写,文件拷贝和文件
- 关于文件读写操作中ios::app与ios::ate的区别
- jscript读写TEXT文件示例(转)
- ios中使用fstream方式打开文件
- 文件操作ofstream,open,close,ifstream,fin,依照行来读取数据, fstream,iosin iosout,fio.seekg(),文件写入和文件读写,文件拷贝和文件
- jscript读写TEXT文件示例
- C++文件读写示例fstream
- python 文件读写模式r,r+,w,w+,a,a+的区别(附代码示例)
- fstream 默认以字符方式读取和写文件,除非设置ios::binary
- 最简单的iOS文件读写示例
- jscript读写TEXT文件示例
- fstream提供了三个类,用来实现c++对文件的操作。(文件的创建、读、写)。 ifstream -- 从已有的文件读 ofstream -- 向文件写内容 fstream - 打开文件供读写
- C++文件读写 打开方式等比较全
- IOS 常用转换与文件读写
- 【iOS】第03讲 文件读写/NSArray/NSData/NSFileManager/NSFileHandle