c++ - hFind 返回 INVALID_HANDLE_VALUE

标签 c++

对于我在论坛上的第一篇帖子,因为我是一个真正的初学者,所以我看不出我的错误从何而来。在用户选择文件夹中的多个文件后,我尝试将它们重命名为“image_1.jpg”、“image_2.jpg”等... 我认为我的代码很好,但我可能犯了一个错误,因为它没有重命名,因为 hFind 总是 INVALID_HANDLE_VALUE。你能帮我看看那是从哪里来的吗?非常感谢!!

#include "stdafx.h"
#include "Accueil.h"
#include <iostream>
#include <string>
#include <math.h>
#include "calib_cam_relative.h"
#include "calib_las_veh.h"
#include "calib_cam_las.h"
#include "calib_interne.h"
#include <stdio.h>
#include <strsafe.h>
#include <stdlib.h>
#include <fstream>
#include <sstream>
#include <windows.h>
#include<shellapi.h>
#include<conio.h> 


                     string cam;
         if(radioButton1->Checked == true){
             cam="CDB";
         }else if(radioButton2->Checked == true){
             cam="CDH";
         }else if(radioButton3->Checked == true){
             cam="CGB";
         }else if(radioButton3->Checked == true){
             cam="CGH";
         }

         string sfol;
         ifstream fich_chemin("chemin.txt");
         getline(fich_chemin,sfol); 
         fich_chemin.close();

         WIN32_FIND_DATA ffd;
         string Dossier = sfol+"\\calib_interne\\TOOLBOX_calib\\" + cam + "\\*.jpg";
         cout << Dossier << endl;
         TCHAR* dirname =  (TCHAR*)Dossier.c_str();
         HANDLE hFind;
         hFind = FindFirstFile((LPCWSTR)Dossier.c_str(), &ffd);
         if (hFind == INVALID_HANDLE_VALUE){
             cout << "erreur" << endl;
         }
         LPTSTR oldfilename;
         TCHAR newfile[260];
         LPTSTR newfilename = &newfile[0];

         int i=1;
         while (FindNextFile(hFind, &ffd)!=0)
         {
            cout << "le premier fichier est " << ffd.cFileName << endl;
            oldfilename=ffd.cFileName;
            StringCchPrintf(newfilename,8,TEXT("%s\\image_%d.jpg"),dirname, i);
            BOOL rs = MoveFile(oldfilename,newfilename);
            i++;
         }
         FindClose(hFind);

感谢您的回答!这是解决方案!

string cam;
             if(radioButton1->Checked == true){
                 cam="CDB";
             }else if(radioButton2->Checked == true){
                 cam="CDH";
             }else if(radioButton3->Checked == true){
                 cam="CGB";
             }else if(radioButton4->Checked == true){
                 cam="CGH";
             }else if(radioButton5->Checked == true){
                 cam="CF";
             }

             string sfol;
             ifstream fich_chemin("chemin.txt");
             getline(fich_chemin,sfol); 
             fich_chemin.close();

             WIN32_FIND_DATAA ffd;
             HANDLE hFind;
             string Dossier = sfol+"\\calib_interne\\TOOLBOX_calib\\" + cam + "\\*.jpg";
             //string Dossier = "D:\\*.jpg";
             TCHAR* dirname =  (TCHAR*)Dossier.c_str();
             hFind = FindFirstFileA((LPCSTR)Dossier.c_str(), &ffd);
             int i=1;
             ostringstream oss;
             oss << i;
             string iss = oss.str();
             string newfilename = sfol + "\\calib_interne\\TOOLBOX_calib\\" + cam + "\\image_" + iss + ".jpg";
             string oldfilename = sfol + "\\calib_interne\\TOOLBOX_calib\\" + cam + "\\" + ffd.cFileName;
             BOOL rs = MoveFileA((LPCSTR)oldfilename.c_str(),(LPCSTR)newfilename.c_str());
             /*if (rs == 0){
                 DWORD dw = GetLastError(); 
                 cout << dw << endl;
             }*/
            while (FindNextFileA(hFind, &ffd)!=0)
            {
                i++;
                ostringstream oss;
                oss << i;
                iss = oss.str();
                newfilename = sfol + "\\calib_interne\\TOOLBOX_calib\\" + cam + "\\image_" + iss + ".jpg";
                oldfilename = sfol + "\\calib_interne\\TOOLBOX_calib\\" + cam + "\\" + ffd.cFileName;
                BOOL rs2 = MoveFileA((LPCSTR)oldfilename.c_str(),(LPCSTR)newfilename.c_str());
                cout << rs2 << endl;
             }
             FindClose(hFind);

最佳答案

string 总是由 char 组成。所以你的代码

     TCHAR* dirname =  (TCHAR*)Dossier.c_str();

仅在使用多字节字符集编译时有效,而在使用 Unicode 字符集编译时无效。你的下一行也不起作用:

     hFind = FindFirstFile((LPCWSTR)Dossier.c_str(), &ffd);

string::c_str 返回一个 const char*。从技术上讲,您可以将此指针转换为 LPCWSTR,但这只会更改指针,而不会更改它指向的字符串。由于 FindFirstFile 的 Unicode 版本需要您提供 Unicode 字符串,因此这种转换没有意义。

你也可以

  • 用多字节字符集编译或
  • 使用 Unicode 字符集编译并使用 ANSI 模式函数(将尾随“A”添加到 Windows API 函数)或
  • 使用 Unicode 字符集编译并使用 wstring 而不是 string
  • 看看 string 和 wstring 是如何类型定义的(两者都是模板的特化,一个使用 char,另一个使用 wchar_t)并定义您自己的 tstring(也是模板的特化,使用 TCHAR)

关于c++ - hFind 返回 INVALID_HANDLE_VALUE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17294798/

相关文章:

c++ - 在 OpenCV、C++ 中创建方形晕影

c++ - 测量 std::function 的性能成本

c++ - 我如何使用 std::shared_ptr 和 box2d?

c++ - 使用运算符重载 c++ 进行监控

c++ - Unicode 异常类

c++ - 设置泛型返回类型

c++ - strcmp 的返回值是什么意思?

python - 从 C++ 运行 python 脚本时内存泄漏

c++ - "Right"从 C++ 中的大端二进制文件检索 int 的方法

c++ - pArray = pArray + 1;有人可以向我解释这如何在 C++ 中增加数组吗?