C/C++ 读写文件

见《C/C++读写文本文件、二进制文件》:https://blog.csdn.net/nichengwuxiao/article/details/78789225

《C++fstream文件流处理对中文字符不支持的解决办法》https://my.oschina.net/abcijkxyz/blog/722482

 

FileDo.h

#pragma once

#include <string>

using namespace std;

void WriteBinFile(const char *filename, string Contain);
string ReadBinFile(const char *filename);
void WriteTxtFile(const char *filename, vector<string> vContain);
void ReadTxtFile(const char *filename, vector<string> &vText);


 

FileDo.cpp

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#define _AFXDLL
#include <afx.h>

#include <locale>

using namespace std;

void WriteBinFile(const char *filename, string Contain)
{
	locale &loc=locale::global(locale(locale(),"",LC_CTYPE));  // 不论以输出文件流还是输入文件流,此操作应放在其两边
        setlocale(LC_ALL,"Chinese-simplified");
	ofstream fout(filename, ios::binary);
	locale::global(loc); //还原全局区域设定
	if(fout.is_open() == false){printf("Error: open failed!!!\n"); while (1);}
	const unsigned int BlockSize = 1024;
	char *buf = (char*)Contain.c_str();
	int i;
	for (i=0; i<Contain.length()/BlockSize; i++)fout.write(buf+BlockSize*i, BlockSize);
	fout.write(buf+BlockSize*i, Contain.length()%BlockSize);
	fout.close();
}

string ReadBinFile(const char *filename)
{
	locale &loc=locale::global(locale(locale(),"",LC_CTYPE));  // 不论以输出文件流还是输入文件流,此操作应放在其两边
        setlocale(LC_ALL,"Chinese-simplified");
	ifstream fin(filename, ios::binary);
	locale::global(loc); //还原全局区域设定
	if(fin.is_open() == false){printf("Error: open failed!!! %s\n", filename); while (1);}
	const unsigned int BlockSize = 1024;
	string strRet;
	char buf[BlockSize];
	for (int i=0; ; i++)
	{
		fin.read(buf, BlockSize);
		int read_size = fin.gcount();
		if (read_size == 0)break;
		strRet += string(buf, read_size);
	}
	fin.close();
#if 1
	printf("《%s》内容为:", filename);
	for (int i=0; i<strRet.length()&&i<16; i++)printf("%02X ", (unsigned char)strRet[i]);printf("等等\n");
#endif
	return strRet;
}

void WriteTxtFile(const char *filename, vector<string> vContain)
{
	locale &loc=locale::global(locale(locale(),"",LC_CTYPE));  // 不论以输出文件流还是输入文件流,此操作应放在其两边
        setlocale(LC_ALL,"Chinese-simplified");
	ofstream fout(filename);
	locale::global(loc); //还原全局区域设定
	if(fout.is_open() == false){printf("Error: open failed!!!\n"); while (1);}
	for (int i=0; i<vContain.size(); i++)fout<<vContain[i]<<endl;
	fout.close();
}

void ReadTxtFile(const char *filename, vector<string> &vText) // 如果文件太大,则不宜用返回值传递,有时如果太大,会很慢,相当于再读一遍的时间。
{
	locale &loc=locale::global(locale(locale(),"",LC_CTYPE));  // 不论以输出文件流还是输入文件流,此操作应放在其两边
        setlocale(LC_ALL,"Chinese-simplified");
	ifstream fin(filename);
	locale::global(loc); //还原全局区域设定
	if(fin.is_open() == false){printf("Error: open failed!!! %s\n", filename); while (1);}
	unsigned long TickOld = GetTickCount();
	int lineCnt = 0;
	for (string strLine; getline(fin, strLine); lineCnt++)
	{
		vText.push_back(strLine);
		if (GetTickCount() - TickOld > 1000)
		{
			TickOld = GetTickCount();
			printf("ReadTxtFile %d line\n", lineCnt);
		}
	}
	fin.close();
	printf("Read file End\n");
}

 

posted on 2019-10-24 15:23  lizhuohui  阅读(41)  评论(0编辑  收藏  举报

导航