Skip to content

Instantly share code, notes, and snippets.

@LusainKim
Last active September 25, 2015 04:09
Show Gist options
  • Save LusainKim/5f8e6d3ab73aa4106ac8 to your computer and use it in GitHub Desktop.
Save LusainKim/5f8e6d3ab73aa4106ac8 to your computer and use it in GitHub Desktop.
FileCopy
#include <windows.h>
#include <conio.h>
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;
#define ONE_READ_BUF_SIZE 512
typedef struct str
{
size_t _size;
char _str[ONE_READ_BUF_SIZE];
str(char* p, size_t size = 0) { _size = size; memcpy(_str, p, ONE_READ_BUF_SIZE); }
str(size_t size = 0) { _size = size; ZeroMemory(_str, ONE_READ_BUF_SIZE); }
}str;
template<typename NumType = unsigned int>
string FixValue(NumType fixnum)
{
string strfix;
int count = 0;
int endnum;
while (fixnum > 0)
{
strfix += ('0' + (fixnum % 10));
fixnum /= 10;
if (fixnum > 0 && ++count % 3 == 0)
strfix += ',';
}
if (count == 0)
return string("0");
return string(strfix.rbegin(),strfix.rend());
}
///////////////////////////////////////////////////////////////////////////
// console cursor
HANDLE consoleHandler = GetStdHandle(STD_OUTPUT_HANDLE);
int gotoxy(int x, int y)
{
if (consoleHandler == INVALID_HANDLE_VALUE)
return 0;
COORD coords = { static_cast<short>(x), static_cast<short>(y) };
SetConsoleCursorPosition(consoleHandler, coords);
return 1;
}
int wherex()
{
if (consoleHandler == INVALID_HANDLE_VALUE)
return 0;
CONSOLE_SCREEN_BUFFER_INFO screenInfo;
GetConsoleScreenBufferInfo(consoleHandler, &screenInfo);
return screenInfo.dwCursorPosition.X;
}
int wherey()
{
if (consoleHandler == INVALID_HANDLE_VALUE)
return 0;
CONSOLE_SCREEN_BUFFER_INFO screenInfo;
GetConsoleScreenBufferInfo(consoleHandler, &screenInfo);
return screenInfo.dwCursorPosition.Y;
}
///////////////////////////////////////////////////////////////////////////
bool LoadFile(vector<str>& newFile)
{
string strFilename;
cout << "input file : ";
cin >> strFilename;
fstream fs(strFilename, ios::in | ios::binary);
fs.imbue(locale("korean"));
// check succese
if (fs.is_open()) cout << strFilename << " is open!" << endl;
else {
cout << strFilename << " is not exist!" << endl;
return false;
}
// size check;
fs.seekg(0, fstream::end);
size_t szData = fs.tellg();
fs.seekg(0);
// draw percent
int nowper = 0;
int size_div_100 = int(szData / 100);
if ((szData % 100) > 0) size_div_100++;
str* pStr = nullptr;
for (int i = 0; i < szData; i+= ONE_READ_BUF_SIZE)
{
newFile.push_back(str(min(szData - i, ONE_READ_BUF_SIZE)));
pStr = &newFile.back();
fs.read(pStr->_str, pStr->_size);
// draw percent
if (nowper != i / (size_div_100))
{
nowper++;
// 커서 초기화
gotoxy(0, wherey());
for (int ido = 0; ido < (nowper / 10); ++ido)
cout << "■";
if (nowper < 100) (nowper % 10 > 0) ? cout << "▤" : cout << "□";
for (int ido = (nowper / 10) + 1; ido < 10; ++ido)
cout << "□";
cout << " : " << nowper << "%";
}
}
fs.close();
// 커서 초기화
gotoxy(0, wherey());
for (int ido = 0; ido < 10; ++ido) cout << "■";
cout << " : 100%";
cout << endl << endl << "complete!" << endl;
cout << strFilename << " is " << FixValue(szData) << " bytes." << endl;
return true;
}
void SaveFile(vector<str>& newFile)
{
string strExtend;
cout << "new program name is : ";
cin >> strExtend;
fstream fn(strExtend, ios::out | ios::binary);
fn.imbue(locale("korean"));
size_t cnt = 0;
size_t szDataBlock = newFile.size();
// draw percent
int nowper = 0;
int szDataBlock_div_100 = int(szDataBlock / 100);
if ((szDataBlock % 100) > 0) szDataBlock_div_100++;
size_t szData = 0;
for (auto p : newFile)
{
fn.write(p._str, p._size);
szData += p._size;
// draw percent
if (nowper != cnt++ / (szDataBlock_div_100))
{
nowper++;
// 커서 초기화
gotoxy(0, wherey());
for (int ido = 0; ido < (nowper / 10); ++ido)
cout << "■";
if(nowper < 100) ( nowper % 10 > 0) ? cout << "▤" : cout << "□";
for (int ido = (nowper / 10) + 1; ido < 10; ++ido)
cout << "□";
cout << " : " << nowper << "%";
}
}
// 커서 초기화
gotoxy(0, wherey());
for (int ido = 0; ido < 10; ++ido) cout << "■";
cout << " : 100%";
cout << endl << endl << "File save succese!" << endl;
cout << "File size is " << FixValue(szData) << " bytes." << endl;
fn.close();
}
int main()
{
vector<str> newFile;
if (!LoadFile(newFile))
{
system("pause");
system("cls");
return main();
}
SaveFile(newFile);
}
@LusainKim
Copy link
Author

STL을 사용한 파일 복사입니다.
실행 파일의 위치에 있는 파일은 파일명과 확장자(ex : File.abc)를 입력하면 파일을 읽습니다. 전체경로를 이용할 수 있습니다.
입력이 완료되고 저장할 파일의 파일명과 확장자를 입력하면 입력한 파일명대로 파일이 저장됩니다.

만약 문제가 발생하였는지 확인하기 위하여 원본 파일과 복사 파일의 바이트 수를 속성 창을 열어 비교하여 보면 검산까지 완료됩니다.

@potris12
Copy link

dpqld에베데베에에베에벱데덷 ㅔstl이네영

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment