c++ 寬字符文件的拷貝

  • copy2.h
#ifndef _COPY2_H_
#define _COPY2_H_

#include <iostream>
#include <string>

using namespace std;

#define MAX_COPY_SIZE 4096 * 32

extern "C"
{
	/*
	* file_copy:   Îļþ¿½±´º¯Êý
	* srcFilePath: Ô´Îļþ·¾¶
	* dstPath:     Ä¿±ê·¾¶
	* return:      ÕýÈ··µ»Ø0£¬ ´íÎó·µ»Ø -1
	*/
	int file_copy(const wstring srcPath, const wstring dstPath);

	long long getcopysize();

	int setcopysize();
}

#endif
  • copy
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>

#include "copy2.h"

static long long copiedSize = 0;

extern "C"
{
	int file_copy(const wstring srcPath, const wstring dstPath)
	{
		wifstream src_file;
		wofstream dst_file;

		src_file.open(srcPath, ios::in | ios::binary);
		dst_file.open(dstPath, ios::out | ios::trunc | ios::binary);

		if (src_file.is_open() != true || dst_file.is_open() != true)
		{
			wcout << L"open " << srcPath << L" or " << dstPath << L" is err " << std::endl;
			return -1;
		}

		src_file.seekg(0, ios::beg);

		wchar_t copyBuffer[MAX_COPY_SIZE];
		streamsize count = 0;
		while (!src_file.eof())
		{
			src_file.read(copyBuffer, MAX_COPY_SIZE);
			count = src_file.gcount();
			dst_file.write(copyBuffer, count);
			copiedSize += count;
		}

		src_file.close();
		dst_file.close();

		return 0;
	}

	long long getcopysize()
	{
		return copiedSize;
	}

	int setcopysize()
	{
		copiedSize = 0;

		return 0;
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章