C++ 获取文件大小、文件扩展名并将其分配到C/C中其他名称下的新文件夹++
我正在用C/C++通过DEV C编写自己的登录转换器++ 它应该更改登录屏幕背景上的图像,但我希望它从用户输入中获取图像,检查它是否小于245KB,以及它是否是*.jpeg文件 理想情况下,它将在set_image函数中完成。 以下是我当前的代码,如果它对您有帮助的话:C++ 获取文件大小、文件扩展名并将其分配到C/C中其他名称下的新文件夹++,c++,c,C++,C,我正在用C/C++通过DEV C编写自己的登录转换器++ 它应该更改登录屏幕背景上的图像,但我希望它从用户输入中获取图像,检查它是否小于245KB,以及它是否是*.jpeg文件 理想情况下,它将在set_image函数中完成。 以下是我当前的代码,如果它对您有帮助的话: #include<stdio.h> #include<windows.h> #include<conio.c> #include<stdlib.h> int patch(){
#include<stdio.h>
#include<windows.h>
#include<conio.c>
#include<stdlib.h>
int patch(){
system("cls");
system("regedit /s ./src/patch.reg");
system("md %windir%/system/oobe/info/backgrounds");
gotoxy(12,35);
printf("Patch Successful!");
return 0;
}
int unpatch(){
system("regedit /s ./src/unpatch.reg");
system("rd /q %windir%/system/oobe/info/backgrounds");
gotoxy(12,35);
printf("Unpatch Successful!");
return 0;
}
int set_image(){
}
main(){
int i;
system("cls");
gotoxy(10,1);
printf("LOGON CHANGER V0.1");
gotoxy(30,10);
printf("1 - Patch");
gotoxy(30,11);
printf("2 - Unpatch");
gotoxy(30,12);
printf("3 - Set Image");
gotoxy(15,25);
printf("Insert your option");
gotoxy(35,25);
scanf("%i",&i);
switch(i){
case 1:
patch();
break;
case 2:
unpatch();
break;
case 3:
set_image();
break;
default:
system("cls");
gotoxy(35,12);
printf("Not a valid input, try again ;)");
Sleep(3000);
main();
}
}
可以在C++中用少量标准库类和实用工具实现这一点。下面的例子应该让您开始学习。您需要更改错误处理并修改它以满足您的特定需要
#include <algorithm>
#include <fstream>
#include <string>
#include <cctype>
bool set_image(
const std::string& inPathname,
const std::string& outPathname,
size_t maxSize)
{
// Validate file extension
size_t pos = inPathname.rfind('.');
if (pos == std::string::npos)
{
return false;
}
// Get the file extension, convert to lowercase and make sure it's jpg
std::string ext = inPathname.substr(pos);
std::transform(ext.begin(), ext.end(), ext.begin(), std::tolower);
if (ext != ".jpg")
{
return false;
}
// open input file
std::ifstream in(inPathname, std::ios::binary);
if (!in.is_open())
{
return false;
}
// check length
in.seekg(0, std::ios::end);
if (in.tellg() > maxSize)
{
return false;
}
in.seekg(0, std::ios::beg);
// open output file
std::ofstream out(outPathname, std::ios::binary);
if (!out.is_open())
{
return false;
}
// copy file
std::copy(
std::istream_iterator<char>(in),
std::istream_iterator<char>(),
std::ostream_iterator<char>(out));
return true;
}
很抱歉,但这不是C++。main函数不能在程序中使用。你为什么不在Windows上搜索如何独立完成这些功能呢,只有当您有实际的代码尝试您讨论的每一点,并且有我们可以帮助您解决的特定问题时,才在此处发布?这些系统调用最好用实际的API调用替换。谢谢: