2013-07-02 85 views
-4

我在写我自己的日志上换用C/C++槽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(); 
     } 
} 
+0

很抱歉的说,但这不是C++。 *函数main不能在程序中使用* – chris

+0

为什么你不能在Windows上独立地执行每一个操作,并且只有当你有实际的代码尝试你讨论的每一点时我们可以帮助您的一个具体问题? –

+0

那些'系统'调用将会更好地被实际的API调用所取代。 – chris

回答

0

您可以在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; 
} 
+0

不经意谢谢:) –