C++ Klasör ve Dosya İşlemleri için Kullanabileceğiniz Bir Sınıf
kopyalamak Klasik dosya işlemleri.
/*----------------------------------------------------------------------
File:CFile.H
Author:Huseyin Bora Abaci
Last Update:24.01.2005
Platform:Windows
Compiler:Borland C++Builder
-----------------------------------------------------------------------*/
--------------------------------------------------------------------------------
-----------------------------------------------------------------------*/
#ifndef _CFile_H_
#define _CFile_H_
//-----------------------------------------------------------------------
#include "stdio.h"
#include "stdlib.h"
//-----------------------------------------------------------------------
#include "dos.h"
#include "dir.h"
//-----------------------------------------------------------------------
#define _MAX_FILE_NAME (500)
#define _MAX_COPY_FNAME (260)
class CFile {
protected:
FILE *In *Out;
__int64 Size;
public:
CFile(){Size=0;}//Kurucu
int FileCopy(char CopyFile[]char NewFile[]);
int FileByte(char ReadFile[]char WriteFile[]long Byte);
int FileSize(char FileName[]);
void __fastcall CopyTree(char FolderName[_MAX_PATH]char Asterik[]char FolderCopy[_MAX_PATH]);
void __fastcall Deltree(char FolderName[]);
void __fastcall MoveTree(char DeleteFolder[_MAX_PATH]char FolderCopy[_MAX_PATH]);
bool FileDelete(char FileName[]);
bool NewFolder(char FolderName[]);
bool DeleteFolder(char FolderName[]);
bool MoveFile(char DeleteFile[]char CopyFile[]);
};
/*----------------------------------------------------------------------------
Dosyanın Bir kopyasını oluşturmak için yazdığım fonksiyon
kopyalama işlemi başarılı olduğunda kopyalanan byte kadar geri döşüş değeri alır
hata durumunda sıfır
----------------------------------------------------------------------------*/
--------------------------------------------------------------------------------
inline int CFile::FileCopy(char CopyFile[]char NewFile[])
{
if((In=fopen(CopyFile"rb"))==NULL)return 0;
if ((Out=fopen(NewFile"wb"))==NULL)return 0;
Size=0;
while(!feof(In))
{
if(!feof(In))putc(getc(In)Out);
Size++;
}
fclose(In);
fclose(Out);
return Size;
}
/*----------------------------------------------------------------------------
Dosyanın Belirtilen Byte ına kadar kopyalayan fonksiyon
kopyalama işlemi başarılı olduğunda kopyalanan byte kadar geri döşüş değeri alır
hata durumunda sıfır
----------------------------------------------------------------------------*/
--------------------------------------------------------------------------------
inline int CFile::FileByte(char ReadFile[]char WriteFile[]long Byte)
{
if((In=fopen(ReadFile"rb"))==NULL)return 0 ;
if((Out=fopen(WriteFile"wb"))==NULL)return 0 ;
Size=0;
while(!feof(In))
{
Size++;
putc(getc(In)Out);
if(Size==Byte)break;
}
fclose(In);
fclose(Out);
return Size;
}
/*----------------------------------------------------------------------------
Dosyanın byte cinsinden boyutunu döndüren fonksiyon
----------------------------------------------------------------------------*/
--------------------------------------------------------------------------------
inline int CFile::FileSize(char FileName[])
{
if((In=fopen(FileName"r+"))==NULL)
return 0;
fseek(In0LSEEK_END);
return ftell(In);
fclose(In);
}
//Klasik Dosya işlemleri
--------------------------------------------------------------------------------
//---------------------------------------------------------------------------
bool CFile::FileDelete(char FileName[])
{
if(remove(FileName))
return true;
}
//----------------------------------------------------------------------------
bool CFile::NewFolder(char FolderName[])
{
if(mkdir(FolderName))
return true;
}
//----------------------------------------------------------------------------
bool CFile:eleteFolder(char FolderName[])
{
if(rmdir(FolderName))
return true;
}
//Dosya Taşı
//----------------------------------------------------------------------------
bool CFile::MoveFile(char DeleteFile[]char CopyFile[])
{
FileCopy(DeleteFileCopyFile);
if(remove(DeleteFile))
return true;
}
/*----------------------------------------------------------------------------
Dizin yapısını olduğu gibi kopyalayan fonksiyon
----------------------------------------------------------------------------*/
--------------------------------------------------------------------------------
void __fastcall CFile:: CopyTree(char FolderName[_MAX_PATH]char Joker[]char FolderCopy[_MAX_PATH])
{
struct ffblk fp;
int States;
char NextNewFolder[_MAX_FILE_NAME] Files1[_MAX_FILE_NAME] Files2[_MAX_FILE_NAME] FA[_MAX_FILE_NAME] Folder[_MAX_FILE_NAME] NextSourceFolder[_MAX_FILE_NAME];
sprintf(FA"%s%s"FolderNameJoker);
States =findfirst(FA&fp0|FA_DIREC|FA_HIDDEN);
while(!States)
{
if(fp.ff_attrib & 0x10) /*FA_DIREC 0x10 Directory */
{
sprintf(Folder"%s%s"FolderCopyfp.ff_name);
if((strcmp(fp.ff_name".")>1) && (strcmp(fp.ff_name"..")>1))
{
mkdir(Folder);
sprintf(NextSourceFolder"%s%s%s"FolderNamefp.ff _name"\\");
sprintf(NextNewFolder"%s%s%s"FolderCopyfp.ff_na me"\\");
CopyTree(NextSourceFolderJokerNextNewFolder);
}
}
sprintf(Files1"%s%s" FolderNamefp.ff_name);
sprintf(Files2"%s%s"FolderCopyfp.ff_name);
FileCopy(Files1Files2);
States=findnext(&fp);
}
findclose(&fp);
}
//------------------------------------------------------------------------
/*Verilen Dizin yapısını olduğu gibi kaldıran fonksiyon*/
//------------------------------------------------------------------------
--------------------------------------------------------------------------------
void __fastcall CFile:eltree(char FolderName[])
{
struct ffblk *fd=new ffblk;
char files[_MAX_FILE_NAME]FA[_MAX_FILE_NAME]Folder[_MAX_FILE_NAME]="";
sprintf(FA"%s%s"FolderName"*.*");
findfirst(FAfd0|FA_DIREC|FA_HIDDEN);
do
{
if(fd->ff_attrib & 0x10) /*FA_DIREC 0x10 Directory */
if((strcmp(fd->ff_name".")>1) && (strcmp(fd->ff_name"..")>1))
{
sprintf(Folder"%s%s%s"FolderNamefd->ff_name"\\");
Deltree(Folder);
if(rmdir(Folder)); //Burda Bir Gecikme Gerikyordu if(rmdir(Folder)) ile gecikme sağlandı
if(rmdir(Folder));
}
if((strcmp(fd->ff_name".")>1) && (strcmp(fd->ff_name"..")>1))
{
sprintf(files"%s%s"FolderNamefd->ff_name);
if(remove(files));
}
}
while(!findnext(fd));
findclose(fd);
}
//Verilen Dizin yapısını taşıyan fonksiyon
--------------------------------------------------------------------------------
void __fastcall CFile::MoveTree(char DeleteFolder[_MAX_PATH]char FolderCopy[_MAX_PATH])
{
NewFolder(FolderCopy);
CopyTree(DeleteFolder"*.*"FolderCopy);
Deltree(DeleteFolder);
rmdir(DeleteFolder);
}
#endif//header dosya sonu
Sınıfınfı nasıl kullanacaksınız
#include <CFile>
main (){
CFile *File1 =new CFile();
File1->CopyTree("C:\\Windows""*.*""D:\\Test");
File->FileByte("C:\\Prog.exe""C:\\200Byte.exe"200); //Dosyanın ilk 200 byte nını kopyalar
__________________
بِسْــــــــــــــــــــــمِ اﷲِارَّحْمَنِ ارَّحِيم
-------------------------------------------------
Bu Soysuzlar Bu Vatansızlar Sarsada Yurdumu Ben Yaratan'dan Alırım Asil Kanı ve Gücü. -------------------------------------------------
|