2011-12-30 46 views
-3

我需要保存从相机捕获的图像,就像 “D:\ storage \ img1”,然后再按“s”另一次,程序应该保存 “D:\ storage \ img2”然后 “D:\ storage \ img3” 所以每次按下自定义键时,它都会保存具有不同名称的图像。 如何做到这一点?用动态名称保存许多图像

感谢您的答复

回答

0

此代码将int i的值连接(增加)到string filename。这是通过IntToStr()完成的。并int i020循环,从而创建您的“动态名称”。

文件输出:

PhotoImage0.txt PhotoImage1.txt PhotoImage2.txt .. .. PhotoImage19.txt

#include <iostream> 
#include <fstream> 
#include <string> 
#include <sstream> 
using namespace std; 

string IntToStr(int n) 
{ 
    stringstream result; 
    result << n; 
    return result.str(); 
} 

int main() 
{ 
    ofstream PhotoImageFile; 
    int Number_of_files=20; 
    string filename; 


    for (int i=0;i<Number_of_files;i++) 
    { 
     filename="c:\\PhotoImage" + IntToStr(i) +".txt"; 
     cout<< filename << " \n"; 

     PhotoImageFile.open(filename.c_str()); 
     PhotoImageFile << filename<<" : Writing this to a file.\n"; 
     PhotoImageFile.close(); 
    } 


    return 0; 
} 
0

在启动时,用“IMG *。*”面具遍历文件夹 - 你怎么做,这是到你的文件系统API。使用字符串函数或循环来提取表示数字的文件名部分并将其转换为int。每次需要保存文件时,将int加1,然后转换回数字串&,然后将路径“img”,数字串和扩展名连接成汇编新文件规范。