깐우의 세상 만들기

(C++) 폴더 및 파일 생성(복사) 하는 방법 본문

Book / Study

(C++) 폴더 및 파일 생성(복사) 하는 방법

깐우 2011. 7. 29. 23:39
파일을 읽어서 다른 이름으로 복사해 그것을 out이라는 폴더를 생성하여 저장

다 짜고 알고보니 rename라는 함수가 있더라...ㅡ,ㅡ

#include<direct.h>

void FilenameChange::ReadFile(int filenumber)
{
ifstream fin;
ofstream fout;

char *buffer;
char filename[255];
char out_filename[255];

sprintf_s(filename,"%d",filenumber);
//소스 파일 연다
fin.open(filename,ios::in | ios::binary);

int size=0;

if(fin.is_open()==true)
{
//소스파일 크기 확인
fin.seekg(0,ios::end);
size=fin.tellg();

//파일 포인터 처음 위치로 이동
fin.seekg(0,ios::beg);
//클리어
fin.clear();
//필요 메모리 할당
buffer=new char[size];

//파일 읽음
fin.read(buffer,size);

//하위 디렉토리 생성
_mkdir("out");

// 출력 파일명 생성
sprintf_s(out_filename,"out\\%d",filenumber+this->Gap_number);
fout.open(out_filename,ios::out |ios::binary);

//파일 복사
fout.write(buffer,size) ;
fout.close();

//메모리 해제
delete [] buffer;
}
else
{
cout<<"Out of memory"<<endl;
exit(0);
}

}