C + + file copy, delete, rename __c++

Source: Internet
Author: User
Tags file copy

I. Copying of documents

#include <iostream>
#include <fstream>
using namespace Std;

int CopyFile (char *sourcefile,char *newfile)
{
Ifstream in;
Ofstream out;
In.open (sourcefile,ios::binary);//Open source file
if (In.fail ())//Open source file failed
{
cout<< "Error 1:fail to open the source file." <<endl;
In.close ();
Out.close ();
return 0;
}
Out.open (newfile,ios::binary);//Create destination file
if (Out.fail ())//Create file failed
{
cout<< "Error 2:fail to create the new file." <<endl;
Out.close ();
In.close ();
return 0;
}
else//Copy Files
{
Out<<in.rdbuf ();
Out.close ();
In.close ();
return 1;
}
}
void Main ()
{
Char source[256],newfile[256];
cout<< "Please enter a file path to copy:" <<endl;
cin>>source;
cout<< "Please enter a new file path:" <<endl;
cin>>newfile;
if (CopyFile (source,newfile))
{
cout<< "File successfully copied ..." <<endl;
}
Else
{
cout<< "File copy failed ..." <<endl;
}
Cin.get ();
Cin.get ();
}


Ii. Deletion of documents

#include <iostream.h>
#include <windows.h>
#include <io.h>

void Main ()
{
Char source[256];//file path
cout<< "Please enter the file path to delete:" <<endl;
cin>>source;

/* _access (char *,int) to determine whether a file exists
There is a return of 0, there is no return-1.
_access (const char *path,int mode)
Value of Mode:
00 Is there
02 Write permission
04 Read permission
06 Read and Write permissions
*/
if (!_access (source,0))//If file exists: file is read-only cannot be deleted
{
Remove file read-only property
SetFileAttributes (source,0);
if (DeleteFile (source))//delete succeeded
{
cout<<source<< "was deleted successfully." <<endl;
}
else//cannot be deleted: file read-only or no permissions to perform deletion
{
cout<<source<< cannot be deleted: The file is read-only or has no delete permission. <<endl;
}
}
else//file does not exist
{
cout<<source<< "does not exist and cannot be deleted." <<endl;
}

Cin.get ();
}


Renaming of three files

#include <iostream.h>
#include <windows.h>
#include <io.h>

void Main ()
{
Char source[256];//file path
Char newname[256];
cout<< "Please enter the path to the file you want to rename:" <<endl;
cin>>source;
cout<< "Please enter a new name for the file:" <<endl;
cin>>newname;

if (!_access (source,0))//If file exists:
{
if (!rename (source,newname))//delete succeeded
{
cout<<source<< "successfully renamed to:" <<newname<<endl;
}
else//cannot be renamed: File open or no permission to perform renaming
{
The cout<< file could not be renamed (possibly for the following reasons): <<endl;
cout<< "T" << "1. "<<newname<< already exists" <<endl
<< "T" << "2. "<<newname<< is in use, not closed." <<endl
<< "T" << "3. "<<" You do not have permission to rename this file. " <<endl;
}
}
else//file does not exist
{
cout<<source<< "does not exist and cannot be renamed." <<endl;
}
Cin.get ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.