5个回答
2016-01-30 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
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;
}
#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;
}
展开全部
MoveFile移动文件或者CopyFil复制文件
用 SHFileOperation,这是Windows资源管理器使用的文件操作函数,具体的参考
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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()的注释。
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()的注释。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
很简单啊,建议去看MSDN文件操作相关的类,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询