char img1_file[] = "D:\\project\\pic\\d.jpg";
  char img2_file[] = "D:\\project\\pic\\d.jpg";
  这样,我获得的是制定路径下的文件名。有一堆照片,分别以001.jpg  002.jpg  003.jpg  ……排列
  我每次只对顺序相连的两幅jpg进行处理。请问文件名那我该怎么定义呢?能让 img1_file[]得到001.jpg,
  img2_file[] 得到002.jpg 。处理完后,img1_file[]自动得到003.jpg ,img2_file[]自动得到004.jpg。
  以此类推。有没有什么好的方法,或者直接函数。

解决方案 »

  1.   

    if(x<10)
    sprintf(img1_file[0],"D:\\project\\pic\\00%d.jpg" ,x)
    if(x<100)
    sprintf(img1_file[0],"D:\\project\\pic\\0%d.jpg" ,x)
    if(x<1000)
    sprintf(img1_file[0],"D:\\project\\pic\\%d.jpg" ,x)
      

  2.   

    if(x<10)
    sprintf(img1_file[0],"D:\\project\\pic\\00%d.jpg" ,x)
    if(x>=10&&x<100)
    sprintf(img1_file[0],"D:\\project\\pic\\0%d.jpg" ,x)
    if(x>=100&&x<1000)
    sprintf(img1_file[0],"D:\\project\\pic\\%d.jpg" ,x)
      

  3.   

    CString strPath = "D:\\project\\pic\\";
    CString strFile2;
    CString strFile2;
    while(int i = 0; i<xxxx; i++)
    {
        strFile1.Format("%s%3d.jpg", strPath, 2*i+1);
        strFile2.Format("%s%3d.jpg", strPath, 2*i+2);
    }CString 是很好用的东西
      

  4.   


    %s%3d.jpg 编译错误,而且我用的是OPENCV,定义的时候是CHAR类型的文件名。怎么更改呢?
      

  5.   

    这个可行,不过方法很多,楼主可以Google批量处理文件就可以了
      

  6.   

    如果你没用有unicode就应该没有什么问题
      

  7.   

     我并不是要输出,而是需要输入,载入的图片能自动添加,img1_file[] 从1 添加到3。一次我只要输入两张图,处理结束后,能自动换另外两幅图。
      

  8.   


    CString strPath = "D:\\project\\pic\\";
    strFile1.Format("%s%3d.jpg", strPath, 2*i+1);
    改成
    CString strPath = _T("D:\\project\\pic\\");
    strFile1.Format(_T("%s%3d.jpg"), strPath, 2*i+1);
      

  9.   

    那这个问题就比较麻烦了
    用了unicode的话,CString就是宽字符的,而opencv不是。那就老老实实的用运行时库里的函数吧
      

  10.   

    Converts a sequence of wide characters to a corresponding sequence of multibyte characters.
    size_t wcstombs( char *mbstr, const wchar_t *wcstr, size_t count );
      

  11.   


        char szPath[MAX_PATH] = "D:\\project\\pic\\";
        char szFile1[MAX_PATH];
        char szFile2[MAX_PATH];
        while(int i = 0; i<xxxx; i++)
        {
            sprintf_s(szFile1, "%s%3d.jpg", strPath, 2*i+1);
            sprintf_s(szFile2, "%s%3d.jpg", strPath, 2*i+2);
        }
      

  12.   

     因为我使用了cvloadimg() 而这个函数定义是需要char*类型的。
      

  13.   

    那个什么,把while改成for
    还有sprintf_s的具体参数看下msdn最近用while用得有点多。
      

  14.   


    error C2665: 'sprintf_s' : none of the 2 overloads could convert all the argument types
      

  15.   

          sprintf_s(szFile2, MAX_PATH, "%s%3d.jpg", strPath, 2*i+2);
      

  16.   

    sprintf_s(szFile2, MAX_PATH, "%s%3d.jpg", szPath, 2*i+2);
    这么几行代码,这么多错误,无语。。
      

  17.   

     我把sprintf_s改成了printf ,断点测试后,szFile1并没得到指定路径下的文件。
      

  18.   

    error C2665: 'sprintf_s' : none of the 2 overloads could convert all the argument types
      

  19.   

    sprintf_s(szFile2, MAX_PATH, "%s%30d.jpg", szPath, 2*i+2);汗,还有错误
      

  20.   


    char szPath[MAX_PATH] = "D:\\project\\pic\\";
    char strFile1[MAX_PATH];
    char strFile2[MAX_PATH];
    for( int j = 1; j<2; j++)
    {
    sprintf_s(strFile1,MAX_PATH, _T("%s%30d.jpg"), szPath, 2*j+1);
    sprintf_s(strFile2,MAX_PATH, _T("%s%30d.jpg"), szPath, 2*j+2);
    }error C2665: 'sprintf_s' : none of the 2 overloads could convert all the argument types
      

  21.   


    #define MAX_COUNT 1000char img1_file[MAX_PATH] = {0};
    char img2_file[MAX_PATH] = {0};
    for(int i=0; i != MAX_COUNT; i+=2)
    {
     sprintf_s(img1_file, sizeof(img1_file), "D:\\project\\pic\\%03d.jpg"),i);
     sprintf_s(img2_file, sizeof(img2_file), "D:\\project\\pic\\%03d.jpg"),i+1);
    }
      

  22.   


    不要用_T了
    sprintf_s接受的是char*
    你定义了Unicode _T之后是wchar_t*
      

  23.   

     去掉_T后,“。jpg”识别不出来。报错。
      

  24.   


    error C2665: 'sprintf_s' : none of the 2 overloads could convert all the argument types
      

  25.   

    怎么会呢我这里的代码能运行,输出结果也对#include "windows.h"
    #include "stdio.h"void main()
    {
        char szPath[MAX_PATH] = "D:\\project\\pic\\";
        char szFile1[MAX_PATH];
        char szFile2[MAX_PATH];
        for(int i = 0; i<2; i++)
        {
            sprintf_s(szFile1, MAX_PATH, "%s%03d.jpg", szPath, 2*i+1);
            sprintf_s(szFile2, MAX_PATH, "%s%03d.jpg", szPath, 2*i+2);
            printf(szFile1);
            printf(szFile2);
        }
    }
    结果:
    D:\project\pic\001.jpgD:\project\pic\002.jpgD:\project\pic\003.jpgD:\project\pic
    \004.jpg请按任意键继续. . .
      

  26.   

     好了,还是原来的问题。
    error C2665: 'sprintf_s' : none of the 2 overloads could convert all the argument types
      

  27.   

     对了,我用的是MFC,基于对话框。意思是按下按钮后,能自动换图片,并载入。
    我这显示还是。error C2665: 'sprintf_s' : none of the 2 overloads could convert all the argument types
    真的不好意思,麻烦大哥了。
      

  28.   

    _CRTIMP_ALTERNATIVE __checkReturn_opt int __cdecl sprintf_s(__out_bcount_z(_DstSize) char * _DstBuf, __in size_t _DstSize, __in_z __format_string const char * _Format, ...);
      

  29.   


    sprintf_s(szFile1, MAX_PATH, "%s%03d.jpg", szPath, 2*i+1);
    sprintf_s(szFile2, MAX_PATH, "%s%03d.jpg", szPath, 2*i+2);