用C++如何实现将一个文件夹中的图片移到另外一个文件夹

也可以是拷贝到另外的文件夹

C++实现将一个文件夹中的图片移到另外一个文件夹方法:
#include <stdio.h>
#include <windows.h>
#include <fstream>
#include <iostream>
#include <string>
#include <cassert>
#include <vector>
using namespace std;

bool fileExists(const std::string& fileName)
{
std::fstream file;
file.open(fileName.c_str(), std::ios::in);
if (file.is_open() == true)
{
file.close();
return true;
}
file.close();
return false;
}

static void copyFile(const std::string& fileNameFrom, const std::string& fileNameTo)
{
assert(fileExists(fileNameFrom));
std::ifstream in (fileNameFrom.c_str());
std::ofstream out (fileNameTo.c_str());
out << in.rdbuf();
out.close();
in.close();
}

bool checkFirst()
{
char* cSplash = "Splash.jpg";
//char* tSplash = "C:\ijji\ENGLISH\Gunz\Splash.jpg";

DWORD attr = GetFileAttributes(cSplash);
if (attr = INVALID_FILE_ATTRIBUTES || (attr & FILE_ATTRIBUTE_DIRECTORY))
return 1;
else
return 0;
}

bool checkSecond()
{
char* tSplash = "C:\\ijji\\ENGLISH\\Gunz\\Splash.jpg";
DWORD attr = GetFileAttributes(tSplash);
if (attr = INVALID_FILE_ATTRIBUTES || (attr & FILE_ATTRIBUTE_DIRECTORY))
return 1;
else
return 0;
}

int main(int argc, char** argv)
{
SetConsoleTitle("Beta Installer 1.0 by Reece Dizon");

string cSplash = "Splash.jpg";
string tSplash = "C:\\ijji\\ENGLISH\\Gunz\\Splash.jpg";

string path = argv[0];

menu:
HANDLE hConsole;
hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
int wWhite = 15;
SetConsoleTextAttribute(hConsole, wWhite);
cout << " --------------------------------------------" << endl;
cout << " Installer 1.0" << endl;
cout << " --------------------------------------------\n" << endl;
cout << "0 - Exit" << endl;
cout << "1 - Install\n" << endl;

string mInput;
cout << "Choose a numer and hit enter to proceed." << endl;
cin >> mInput;
if (mInput == "1")
{
if (checkFirst() == 1) //checkFirst() == 1
{
if (checkSecond() == 1 )
{
system("cls");
if (remove(tSplash.c_str()) != 0)
{
//
}
else
{
bool flag = false;
fstream fin;
fin.open(tSplash.c_str()); //tSplash,ios::n
if (fin.is_open())
{
cout << "File was unable to be removed" << endl;
flag = true;
fin.close();
system("pause>nul");
goto Kill;
}
else
{
cout << "File removed.\n" << endl;
cout << "Continue with installation...." << endl;
fin.close();
system("pause>nul");
goto nMove;
}
}
}
}
else
{
cout << "File not found...\n";
system("pause>nul");
goto Kill;
}
}
else if (mInput == "0")
{
goto Kill;
}
else
{
cout << "That was not an option, please try again!" << endl;
system("pause>nul");
}
system("cls");
goto menu;

nMove:
system("cls");
int result;
copyFile(cSplash, tSplash, TRUE);
cout << "Installation successful!" << endl;
system("pause>nul");
goto Kill;

Kill:
system("cls");
return 0;

}
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-09-02

    MoveFile移动文件或者CopyFil复制文件

    用 SHFileOperation,这是Windows资源管理器使用的文件操作函数,具体的参考

    http://baike.baidu.com/view/1719618.htm?fr=aladdin

本回答被提问者采纳
第2个回答  2015-10-30
int rename (FromPath, ToPath);
The rename function renames the file or directory specified by oldname
to the name given by newname. The old name must be the path of an
existing file or directory. The new name must not be the name of an
existing file or directory. You can use rename to move a file from one
directory or device to another by giving a different path in the newname
argument. However, you cannot use rename to move a directory.
Directories can be renamed, but not moved.
这是msdn对rename()的注释。
第3个回答  2015-09-29
很简单啊,建议去看MSDN文件操作相关的类,
第4个回答  2014-05-30
copyfile()
相似回答