android 文件读写exlc,TXT文件中写入中文

程序:

CStdioFile file;

file.Open(szDir,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite);

char* old_locale = _strdup(

setlocale(LC_CTYPE,NULL) );

setlocale( LC_CTYPE, "chs"

);//设定

//file.WriteString(_T("abc你好"));//正常写入

file.WriteString(curpath);//正常写入

setlocale( LC_CTYPE, old_locale

);

free( old_locale

);//还原区域设定

在VC2005环境下,

以下代码无法实现使用CStdioFile向文本文件中写入中文(用notepad.exe查看不到写入的中文)

CStdioFile file;

file.Open(…);

file.WriteString(_T("abc你好"));//只能写入abc

在VC2005中的解决办法:

使用setlocale语句设定区域

#include //头文件

CStdioFile file;

file.Open(…);

char* old_locale = _strdup( setlocale(LC_CTYPE,NULL) );

setlocale( LC_CTYPE, "chs" );//设定

file.WriteString(_T("abc你好"));//正常写入

setlocale( LC_CTYPE, old_locale );

free( old_locale );//还原区域设定

简化处理可以仅使用语句setlocale( LC_CTYPE, "chs" )。

setlocale:

函数原形为:char *setlocale( int category, const char *locale );

头文件:

所支持的操作系统为:ANSI, Win 95, Win NT

对于简体中文可以使用如下设置:setlocale( LC_ALL, "chs" );

为什么一定要调用setlocale呢?

因为在C/C++语言标准中定义了其运行时的字符集环境为"C",也就是ASCII字符集的一个子集,那么mbstowcs在工作时会将cstr中所包含的字符串看作是ASCII编码的字符,而不认为是一个包含有chs编码的字符串,所以他会将每一个中文拆成2个ASCII编码进行转换,这样得到的结果就是会形成4个wchar_t的字符组成的串,那么如何才能够让mbstowcs正常工作呢?在调用mbstowcs进行转换之间必须明确的告诉mbstowcs目前cstr串中包含的是chs编码的字符串,通过setlocale(

LC_ALL, "chs"

)函数调用来完成,需要注意的是这个函数会改变整个应用程序的字符集编码方式,必须要通过重新调用setlocale( LC_ALL,

"C"

)函数来还原,这样就可以保证mbstowcs在转换时将cstr中的串看作是中文串,并且转换成为2个wchar_t字符,而不是4个。

本地化设置需要具备三个条件:

a. 语言代码 (Language Code)

b. 国家代码 (Country Code)

c. 编码(Encoding)

本地名字可以用下面这些部分来构造:

语言代码_国家代码.编码 比如(zh_CN.UTF-8, en_US等)

locale的别名表见 /usr/lib/X11/locale/locale.alias(以Debian

GNU/Linux为例)

setlocale语言字符串参考

另外还有一种方法就是重新写CStdioFile的派生类CStdioFileEx(网上有)。

//好像C++中没有类能够读些Unicode格式的文本文件,所以我写了下面这个类。用法很简单,大家尝试几下就明白了。

#pragma once

class CStdioFileEx: public CStdioFile

{

public:

CStdioFileEx();

CStdioFileEx( LPCTSTR lpszFileName, UINT nOpenFlags );

virtual BOOL Open( LPCTSTR lpszFileName, UINT nOpenFlags,

CFileException* pError = NULL );

virtual BOOL ReadString(CString& rString);

BOOL ReadWideString(CStringW& rString);

BOOL ReadAnsiString(CStringA& rString);

virtual void WriteString(LPCTSTR lpsz);

void WriteWideString(LPCWSTR lpsz);

void WriteAnsiString(LPCSTR lpsz);

bool IsUnicodeFormat() {return m_bIsUnicodeText;}

unsigned long GetCharCount();

// Additional flag to allow Unicode text format writing

enum {modeWriteUnicode = 0x100000};

static bool IsFileUnicode(const CString& sFilePath);

protected:

UINT PreprocessFlags(const CString& sFilePath, UINT&

nOpenFlags);

bool m_bIsUnicodeText;

};

//。cpp文件

#include "stdafx.h"

#include "StdioFileEx.h"

//在UCS 编码中有一个叫做"ZERO WIDTH NO-BREAK

SPACE"的字符,它的编码是FEFF。而FFFE在UCS中是不存在的字符,

//所以不应该出现在实际传输中。UCS规范建议我们在传输字节流前,先传输字符"ZERO WIDTH NO-BREAK

SPACE"。这样

//如果接收者收到FEFF,就表明这个字节流是Big-Endian的;如果收到FFFE,就表明这个字节流是Little-Endian的。

//因此字符"ZERO WIDTH NO-BREAK SPACE"又被称作BOM。

//UTF-8不需要BOM来表明字节顺序,但可以用BOM来表明编码方式。字符"ZERO WIDTH NO-BREAK

SPACE"的UTF-8编码是

//EF BB BF。所以如果接收者收到以EF BB BF开头的字节流,就知道这是UTF-8编码了。

//Windows就是使用BOM来标记文本文件的编码方式的。

//有些老的浏览器和文本编辑器不支持BOM。

#define UNICODE_BOM 0xFEFF//Unicode "byte order mark" which goes

at start of file

CStdioFileEx::CStdioFileEx(): CStdioFile()

{

m_bIsUnicodeText = false;

}

CStdioFileEx::CStdioFileEx(LPCTSTR lpszFileName,UINT

nOpenFlags)

:CStdioFile(lpszFileName, PreprocessFlags(lpszFileName,

nOpenFlags))

{

}

BOOL CStdioFileEx::Open(LPCTSTR lpszFileName,UINT

nOpenFlags,CFileException* pError )

{

PreprocessFlags(lpszFileName, nOpenFlags);

return CStdioFile::Open(lpszFileName, nOpenFlags, pError);

}

BOOL CStdioFileEx::ReadString(CString& rString)

{

#ifdef _UNICODE

return ReadWideString(rString);

#else

return ReadAnsiString(rString);

#endif

}

BOOL CStdioFileEx::ReadWideString(CStringW& rString)

{

_ASSERTE(m_pStream);

rString = L""; // empty string without deallocating

if(m_bIsUnicodeText)

{

// If at position 0, discard byte-order mark before reading

if(GetPosition() == 0)

{

wchar_t bom;

Read(&bom, sizeof(wchar_t));

}

const int nMaxSize = 128;

LPWSTR lpsz = rString.GetBuffer(nMaxSize);

LPWSTR lpszResult;

int nLen = 0;

for (;;)

{

lpszResult = fgetws(lpsz, nMaxSize+1, m_pStream);

rString.ReleaseBuffer();

// handle error/eof case

if (lpszResult == NULL && !feof(m_pStream))

{

Afx_clearerr_s(m_pStream);

AfxThrowFileException(CFileException::genericException,

_doserrno,

m_strFileName);

}

// if string is read completely or EOF

if (lpszResult == NULL ||

(nLen = (int)lstrlenW(lpsz)) < nMaxSize ||

lpsz[nLen-1] == '\n')

break;

nLen = rString.GetLength();

lpsz = rString.GetBuffer(nMaxSize + nLen) + nLen;

}

//remove crlf if exist.

nLen = rString.GetLength();

if (nLen > 1 && rString.Mid(nLen-2) == L"\r\n")

{

rString.GetBufferSetLength(nLen-2);

}

return rString.GetLength() > 0;

}

else

{

CStringA ansiString;

BOOL bRetval = ReadAnsiString(ansiString);

//setlocale(LC_ALL, "chs_chn.936");//no need

rString = ansiString;

return bRetval;

}

}

BOOL CStdioFileEx::ReadAnsiString(CStringA& rString)

{

_ASSERTE(m_pStream);

rString = ""; // empty string without deallocating

if(!m_bIsUnicodeText)

{

const int nMaxSize = 128;

LPSTR lpsz = rString.GetBuffer(nMaxSize);

LPSTR lpszResult;

int nLen = 0;

for (;;)

{

lpszResult = fgets(lpsz, nMaxSize+1, m_pStream);

rString.ReleaseBuffer();

// handle error/eof case

if (lpszResult == NULL && !feof(m_pStream))

{

Afx_clearerr_s(m_pStream);

AfxThrowFileException(CFileException::genericException,

_doserrno,

m_strFileName);

}

// if string is read completely or EOF

if (lpszResult == NULL ||

(nLen = (int)lstrlenA(lpsz)) < nMaxSize ||

lpsz[nLen-1] == '\n')

break;

nLen = rString.GetLength();

lpsz = rString.GetBuffer(nMaxSize + nLen) + nLen;

}

//remove crlf if exist.

nLen = rString.GetLength();

if (nLen > 1 && rString.Mid(nLen-2) == "\r\n")

{

rString.GetBufferSetLength(nLen-2);

}

return rString.GetLength() > 0;

}

else

{

CStringW wideString;

BOOL bRetval = ReadWideString(wideString);

//setlocale(LC_ALL, "chs_chn.936");//no need

rString = wideString;

return bRetval;

}

}

// Purpose: Writes string to file either in Unicode or

multibyte, depending on whether the caller specified the

// CStdioFileEx::modeWriteUnicode flag. Override of base class

function.

void CStdioFileEx::WriteString(LPCTSTR lpsz)

{

#ifdef _UNICODE

WriteWideString(lpsz);

#else

WriteAnsiString(lpsz);

#endif

}

void CStdioFileEx::WriteWideString(LPCWSTR lpsz)

{

ASSERT(lpsz != NULL);

if (lpsz == NULL)

{

AfxThrowInvalidArgException();

}

if(m_bIsUnicodeText)

{

ASSERT(m_pStream != NULL);

// If writing Unicode and at the start of the file, need to

write byte mark

if(GetPosition() == 0)

{

wchar_t cBOM = (wchar_t)UNICODE_BOM;

CFile::Write(&cBOM, sizeof(wchar_t));

}

if (fputws(lpsz, m_pStream) == _TEOF)

AfxThrowFileException(CFileException::diskFull, _doserrno,

m_strFileName);

}

else

{

USES_CONVERSION;

WriteAnsiString(CW2A(lpsz));

}

}

void CStdioFileEx::WriteAnsiString(LPCSTR lpsz)

{

ASSERT(lpsz != NULL);

if (lpsz == NULL)

{

AfxThrowInvalidArgException();

}

if(!m_bIsUnicodeText)

{

ASSERT(m_pStream != NULL);

if (fputs(lpsz, m_pStream) == _TEOF)

AfxThrowFileException(CFileException::diskFull, _doserrno,

m_strFileName);

}

else

{

USES_CONVERSION;

WriteWideString(CA2W(lpsz));

}

}

UINT CStdioFileEx::PreprocessFlags(const CString& sFilePath,

UINT& nOpenFlags)

{

m_bIsUnicodeText = false;

// If we have writeUnicode we must have write or writeRead as

well

if (nOpenFlags & CStdioFileEx::modeWriteUnicode)

{

ASSERT(nOpenFlags & CFile::modeWrite || nOpenFlags &

CFile::modeReadWrite);

m_bIsUnicodeText = true;

}

// If reading in text mode and not creating...

else if (nOpenFlags & CFile::typeText &&

!(nOpenFlags & CFile::modeCreate) && !(nOpenFlags &

CFile::modeWrite ))

{

m_bIsUnicodeText = IsFileUnicode(sFilePath);

}

//如果要读写Unicode格式的文本文件, 必须切换到typeBinary方式,

因为这会影响fputws/fgetws的工作方式(具体情况参考MSDN)。

if (m_bIsUnicodeText)

{

nOpenFlags &= ~(CFile::typeText);

nOpenFlags |= CFile::typeBinary;

}

return nOpenFlags;

}

// Purpose: Determines whether a file is Unicode by reading the

first character and detecting

// whether it's the Unicode byte marker.

bool CStdioFileEx::IsFileUnicode(const CString&

sFilePath)

{

CFile file;

wchar_t cFirstChar;

CFileException exFile;

bool bIsUnicode = false;

// Open file in binary mode and read first character

if (file.Open(sFilePath, CFile::typeBinary | CFile::modeRead,

&exFile))

{

// If byte is Unicode byte-order marker, let's say it's

Unicode

if (file.Read(&cFirstChar, sizeof(wchar_t)) > 0

&& cFirstChar == (wchar_t)UNICODE_BOM)

{

bIsUnicode = true;

}

file.Close();

}

else

{

// Handle error here if you like

}

return bIsUnicode;

}

unsigned long CStdioFileEx::GetCharCount()

{

int nCharSize;

unsigned long nByteCount, nCharCount = 0;

if (m_pStream)

{

// Get size of chars in file

nCharSize = m_bIsUnicodeText ? sizeof(wchar_t):

sizeof(char);

// If Unicode, remove byte order mark from count

nByteCount = (unsigned long)GetLength();

if (m_bIsUnicodeText)

{

nByteCount = nByteCount - sizeof(wchar_t);

}

// Calc chars

nCharCount = (nByteCount / nCharSize);

}

return nCharCount;

}

CFile

//创建/打开文件

CFile file;

file.Open(_T("test.txt"),CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite);

文件打开模式可组合使用,用“|”隔开,常用的有以下几种:

CFile::modeCreate:以新建方式打开,如果文件不存在,新建;如果文件已存在,把该文件长度置零,即清除文件原有内容。

CFile::modeNoTruncate:以追加方式打开,如果文件存在,打开并且不将文件长度置零,如果文件不存在,会抛出异常。一般与CFile::modeCreate一起使用,则文件不存在时,新建一个文件;存在就进行追加操作。

CFile::modeReadWrite:以读写方式打开文件。

CFile::modeRead:只读。

CFile::modeWrite:只写。

//写入数据

CString strValue = "Hello World!";

file.Write(strValue,strValue.GetLength());

//追加数据

file.SeekToEnd(); //将指针移至文件末尾进行追加

file.Write(strValue,strValue.GetLength());

//关闭文件

file.Close();

CStdioFile

CStdioFile是CFile的派生类,对文件进行流式操作,对于文本文件的读写很有用处,可按行读取写入。

//写入数据

CString strValue = "Hello World!";

file.WriteString(strValue);

//读取数据

CString strRead;

file.ReadString(strRead);

当文件存在多行数据需要逐行读取时,可用函数BOOL CStdioFile::ReadString(CString&

rString),当遇到"/n "时读取截断,如果文件未读完,返回true,否则返回false。

//逐行读取文件内容,存入strRead

while(file.ReadString(strRead))

{

...;

}

各种关于文件的操作在程序设计中是十分常见,如果能对其各种操作都了如指掌,就可以根据实际情况找到最佳的解决方案,从而在较短的时间内编写出高效的代码,因而熟练的掌握文件操作是十分重要的。本文将对Visual

C++中有关文件操作进行全面的介绍,并对在文件操作中经常遇到的一些疑难问题进行详细的分析。

1.文件的查找

当对一个文件操作时,如果不知道该文件是否存在,就要首先进行查找。MFC中有一个专门用来进行文件查找的类CFileFind,使用它可以方便快捷地进行文件的查找。下面这段代码演示了这个类的最基本使用方法。

CString strFileTitle;

CFileFind finder;

BOOL bWorking =

finder.FindFile("C://windows//sysbkup//*.cab");

while(bWorking)

{

bWorking=finder.FindNextFile();

strFileTitle=finder.GetFileTitle();

}

2.文件的打开/保存对话框

让用户选择文件进行打开和存储操作时,就要用到文件打开/保存对话框。MFC的类CFileDialog用于实现这种功能。使用CFileDialog声明一个对象时,第一个BOOL型参数用于指定文件的打开或保存,当为TRUE时将构造一个文件打开对话框,为FALSE时构造一个文件保存对话框。

在构造CFileDialog对象时,如果在参数中指定了OFN_ALLOWMULTISELECT风格,则在此对话框中可以进行多选操作。此时要重点注意为此CFileDialog对象的m_ofn.lpstrFile分配一块内存,用于存储多选操作所返回的所有文件路径名,如果不进行分配或分配的内存过小就会导致操作失败。下面这段程序演示了文件打开对话框的使用方法。

CFileDialog mFileDlg(TRUE,NULL,NULL,

OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT,

"All Files (*.*)|*.*||",AfxGetMainWnd());

CString str(" ",10000);

mFileDlg.m_ofn.lpstrFile=str.GetBuffer(10000);

str.ReleaseBuffer();

POSITION mPos=mFileDlg.GetStartPosition();

CString pathName(" ",128);

CFileStatus status;

while(mPos!=NULL)

{

pathName=mFileDlg.GetNextPathName(mPos);

CFile::GetStatus( pathName, status );

}

3.文件的读写

文件的读写非常重要,下面将重点进行介绍。文件读写的最普通的方法是直接使用CFile进行,如文件的读写可以使用下面的方法:

//对文件进行读操作

char sRead[2];

CFile mFile(_T("user.txt"),CFile::modeRead);

if(mFile.GetLength()<2)

return;

mFile.Read(sRead,2);

mFile.Close();

//对文件进行写操作

CFile mFile(_T("user.txt "),

CFile::modeWrite|CFile::modeCreate);

mFile.Write(sRead,2);

mFile.Flush();

mFile.Close();

虽然这种方法最为基本,但是它的使用繁琐,而且功能非常简单。我向你推荐的是使用CArchive,它的使用方法简单且功能十分强大。首先还是用CFile声明一个对象,然后用这个对象的指针做参数声明一个CArchive对象,你就可以非常方便地存储各种复杂的数据类型了。它的使用方法见下例。

//对文件进行写操作

CString strTemp;

CFile mFile;

mFile.Open("d://dd//try.TRY",CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite);

CArchive ar(&mFile,CArchive::store);

ar<

mFile.Close();

//对文件进行读操作

CFile mFile;

if(mFile.Open("d://dd//try.TRY",CFile::modeRead)==0)

return;

CArchive ar(&mFile,CArchive::load);

ar>>strTemp;

ar.Close();

mFile.Close();

CArchive的 << 和>>

操作符用于简单数据类型的读写,对于CObject派生类的对象的存取要使用ReadObject()和WriteObject()。使用CArchive的ReadClass()和WriteClass()还可以进行类的读写,如:

//存储CAboutDlg类

ar.WriteClass(RUNTIME_CLASS(CAboutDlg));

//读取CAboutDlg类

CRuntimeClass* mRunClass=ar.ReadClass();

//使用CAboutDlg类

CObject* pObject=mRunClass->CreateObject();

((CDialog* )pObject)->DoModal();

虽然VC提供的文档/视结构中的文档也可进行这些操作,但是不容易理解、使用和管理,因此虽然很多VC入门的书上花费大量篇幅讲述文档/视结构,但我建议你最好不要使用它的文档。关于如何进行文档/视的分离有很多书介绍,包括非常著名的《Visual

C++ 技术内幕》。

如果你要进行的文件操作只是简单的读写整行的字符串,我建议你使用CStdioFile,用它来进行此类操作非常方便,如下例。

CStdioFile mFile;

CFileException mExcept;

mFile.Open( "d://temp//aa.bat", CFile::modeWrite,

&mExcept);

CString string="I am a string.";

mFile.WriteString(string);

mFile.Close();

4.临时文件的使用

正规软件经常用到临时文件,你经常可以会看到C:/Windows/Temp目录下有大量的扩展名为tmp的文件,这些就是程序运行是建立的临时文件。临时文件的使用方法基本与常规文件一样,只是文件名应该调用函数GetTempFileName()获得。它的第一个参数是建立此临时文件的路径,第二个参数是建立临时文件名的前缀,第四个参数用于得到建立的临时文件名。得到此临时文件名以后,你就可以用它来建立并操作文件了,如:

char szTempPath[_MAX_PATH],szTempfile[_MAX_PATH];

GetTempPath(_MAX_PATH, szTempPath);

GetTempFileName(szTempPath,_T ("my_"),0,szTempfile);

CFile m_tempFile(szTempfile,CFile:: modeCreate|CFile::

modeWrite);

char m_char='a';

m_tempFile.Write(&m_char,2);

m_tempFile.Close();

5.文件的复制、删除等

MFC中没有提供直接进行这些操作的功能,因而要使用SDK。SDK中的文件相关函数常用的有CopyFile()、CreateDirectory()、DeleteFile()、MoveFile()。它们的用法很简单,可参考MSDN。

1,判断文件是否存在

access(filename,mode);

2,对于不同用途又不同的文件操作,其中API函数CreateFile()也是比较有用处理方式,对于巨型文件很合适的其他的楼上的大都说了,不重复了.

[1]显示对话框,取得文件名

CString FilePathName;

CFileDialog dlg(TRUE);///TRUE为OPEN对话框,FALSE为S***E AS对话框

if (dlg.DoModal() == IDOK)

FilePathName=dlg.GetPathName();

相关信息:CFileDialog 用于取文件名的几个成员函数:

假如选择的文件是C:/WINDOWS/TEST.EXE

则(1)GetPathName();取文件名全称,包括完整路径。取回C:/WINDOWS/TEST.EXE

(2)GetFileTitle();取文件全名:TEST.EXE

(3)GetFileName();取回TEST

(4)GetFileExt();取扩展名EXE

[2]打开文件

CFile file("C:/HELLO.TXT",CFile::modeRead);//只读方式打开

//CFile::modeRead可改为 CFile::modeWrite(只写),

//CFile::modeReadWrite(读写),CFile::modeCreate(新建)

例子:

{

CFile file;

file.Open("C:/HELLO.TXT",CFile::modeCreate|Cfile::modeWrite);

.

.

.

}

[3]移动文件指针

file.Seek(100,CFile::begin);///从文件头开始往下移动100字节

file.Seek(-50,CFile::end);///从文件末尾往上移动50字节

file.Seek(-30,CFile::current);///从当前位置往上移动30字节

file.SeekToBegin();///移到文件头

file.SeekToEnd();///移到文件尾

[4]读写文件

读文件:

char buffer[1000];

file.Read(buffer,1000);

写文件:

CString string("自强不息");

file.Write(string,8);

[5]关闭文件

file.Close();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值