C++:读取文本文件到string变量里

可以自动读取文本文件的编码

typedef enum TextFileType
{
    TextFileType_ANSI = 0,
    TextFileType_UNICODE,
    TextFileType_UTF8
}TEXTFILETYPE;



TEXTFILETYPE xPub_GetTextFileType(const CString& strFileName, CString& tFileName)
{
    TEXTFILETYPE fileType = TextFileType_ANSI;
    CFile cfile;
    if (cfile.Open(strFileName, CFile::modeRead | CFile::shareDenyWrite))
    {
        tFileName = cfile.GetFileName();
        char buf[3];
        cfile.Read(buf, 3);
        if ((unsigned char)buf[0] == 0xFF
            && (unsigned char)buf[1] == 0xFE)
        {
            fileType = TextFileType_UNICODE;
        }
        else if ((unsigned char)buf[0] == 0xEF
            && (unsigned char)buf[1] == 0xBB
            && (unsigned char)buf[2] == 0xBF)
        {
            fileType = TextFileType_UTF8;
        }
        cfile.Close();
    }
    return fileType;
}
//读取文件filepath / tFileName 到 变量 string ss里,成功返回1,失败返回0
BOOL xPub_ReadTextFile(CString filepath, CString& tFileName, string& ss)
{
	ss.clear();
	FILE* f = new FILE();
	TEXTFILETYPE fileType = xPub_GetTextFileType(filepath, tFileName);
	errno_t err = _wfopen_s(&f, filepath, L"rb");
	if (err != 0)
		return 0;

	long length = 0;
	fseek(f, 0, SEEK_END);
	length = ftell(f);
	fseek(f, 0, SEEK_SET);

	if (length <= 0)
	{
		fclose(f);
		return 0;
	}

	char* buf; 
	switch (fileType)
	{
	case TextFileType_ANSI:
		break;
	case TextFileType_UNICODE:
		length -= 2;
		fseek(f, 2, 0);
		break;
	case TextFileType_UTF8:
		length -= 3;
		fseek(f, 3, 0);
		break;
	default:
		break;
	}
	buf = new char[length + 1];
	buf[0] = 0;
	int fl = fread(buf, length, 1, f);
	if (fl != 1) {
		fclose(f);
		delete[] buf;
		return 0;
	}
	fclose(f);
	ss.append(buf, length);
	delete(buf);
	return 1;
}

自己的VxTerm所用的其中一个读取会话文件时用的模块。目前运行情况良好。

public static String loadAFileToStringDE1(File f) throws IOException { long beginTime = System.currentTimeMillis(); InputStream is = null; String ret = null; try { is = new BufferedInputStream( new FileInputStream(f) ); long contentLength = f.length(); ByteArrayOutputStream outstream = new ByteArrayOutputStream( contentLength > 0 ? (int) contentLength : 1024); byte[] buffer = new byte[4096]; int len; while ((len = is.read(buffer)) > 0) { outstream.write(buffer, 0, len); } outstream.close(); ret = outstream.toString(); //byte[] ba = outstream.toByteArray(); //ret = new String(ba); } finally { if(is!=null) {try{is.close();} catch(Exception e){} } } long endTime = System.currentTimeMillis(); System.out.println("方法1用时"+ (endTime-beginTime) + "ms"); return ret; } public static String loadAFileToStringDE2(File f) throws IOException { long beginTime = System.currentTimeMillis(); InputStream is = null; String ret = null; try { is = new FileInputStream(f) ; long contentLength = f.length(); byte[] ba = new byte[(int)contentLength]; is.read(ba); ret = new String(ba); } finally { if(is!=null) {try{is.close();} catch(Exception e){} } } long endTime = System.currentTimeMillis(); System.out.println("方法2用时"+ (endTime-beginTime) + "ms"); return ret; } public static String loadAFileToStringDE3(File f) throws IOException { long beginTime = System.currentTimeMillis(); BufferedReader br = null; String ret = null; try { br = new BufferedReader(new FileReader(f)); String line = null; StringBuffer sb = new StringBuffer((int)f.length()); while( (line = br.readLine() ) != null ) { sb.append(line).append(LINE_BREAK); } ret = sb.toString(); } finally { if(br!=null) {try{br.close();} catch(Exception e){} } } long endTime = System.currentTimeMillis(); System.out.println("方法3用时"+ (endTime-beginTime) + "ms"); return ret; } 3个方法去读取一个大于50M的文件,当不设置jvm参数时都OutofMemery,当设置-Xmx128M时。只有方法3 可以通过,设置到-Xmx256M时也只有方法3可以通过,干脆设置512M,都可以了,运行时间如果正常的话一般都在4~5S
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不了阁-飞哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值