gpt4 book ai didi

c++ - 在 C/C++ 中获取文件大小、文件扩展名并将其分配到不同名称的新文件夹中

转载 作者:行者123 更新时间:2023-11-30 21:31:10 24 4
gpt4 key购买 nike

我正在通过 DEV C++ 用 C/C++ 编写自己的日志更改器

它应该更改登录屏幕背景上的图像,但我希望它从用户输入中获取图像,检查它是否在 245KB 以下以及是否是 *.jpeg 文件。

理想情况下,它可以在 set_image() 函数中完成。这是我当前的代码,如果它对您有任何帮助:

#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++ - 在 C/C++ 中获取文件大小、文件扩展名并将其分配到不同名称的新文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17437592/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com