Problem with Files Listing on windows

Hi all of you guys ,
i want to make a listing programe that lists specific folder files into a text file . but i have some problems with unicode file names !!
whene i execute that simple code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<iostream>
#include<conio.h>
#include<fstream>
#include<stdio.h>
#include<stdlib.h>
#include <windows.h>
#include <string>
#include <cstdlib>

int main (int argc, const char * argv[])
{
using namespace std;	
wchar_t short_path[MAX_PATH] ;									
	std::wstring path=L"C://Myfolder/*";  // this is my path that i wish to list
	wfstream scr;	// this for handling our output to a text file	
    WIN32_FIND_DATAW search_data;
    memset(&search_data, 0, sizeof(WIN32_FIND_DATA));
    HANDLE handle = FindFirstFileW(path.c_str(), &search_data);
    int inc=0; // just for counting number of files
    scr.open("screen.txt"); // text file where we want to stor our results 
    while(handle != INVALID_HANDLE_VALUE)
   		{
   			std::wstring sfileName = search_data.cFileName;	
   			//GetShortPathNameW( sfileName.c_str(), short_path, MAX_PATH ) ;  
   			scr<<sfileName<<endl;
   			inc++;
   			if(FindNextFileW(handle, &search_data) == FALSE) break;
	    		
   				}
				   cout<<inc;// gives us 7 which mean it finds all 5 files .	

   FindClose(handle);
   scr.close();
return 0;

}


the output is like that :
https://image.ibb.co/eXDqXa/Screenshot_1.png
then when i try this code (short path code ):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<iostream>
#include<conio.h>
#include<fstream>
#include<stdio.h>
#include<stdlib.h>
#include <windows.h>
#include <string>
#include <cstdlib>

int main (int argc, const char * argv[])
{

	using namespace std;
	wchar_t short_path[MAX_PATH] ;
	std::wstring path=L"C://Myfolder/*";	// this is my path that i wish to list
	wfstream scr;	// this for handling our output to a text file	
    WIN32_FIND_DATAW search_data;
    memset(&search_data, 0, sizeof(WIN32_FIND_DATA));
    HANDLE handle = FindFirstFileW(path.c_str(), &search_data);
    int inc=0; // just for counting number of files
    scr.open("screen.txt"); // text file where we want to stor our results 
    while(handle != INVALID_HANDLE_VALUE)
   		{
   			std::wstring sfileName = search_data.cFileName;	
   			GetShortPathNameW( sfileName.c_str(), short_path, MAX_PATH ) ;  
   			scr<<short_path<<endl;
   			inc++;
   			if(FindNextFileW(handle, &search_data) == FALSE) break;
	    		
   				}
				   cout<<inc;// gives us 7 which mean it finds all 5 files .	

   FindClose(handle);
   scr.close();


return 0;

}


i get good result :
https://image.ibb.co/kJuayF/Screenshot_2.png

please can anyone help me on understanding that ?
and how to list all files even unicode named files ?
thank you all ,
Last edited on
Visual Studio on Windows, using the File System Library.
http://en.cppreference.com/w/cpp/header/filesystem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
#include <vector>
#include <experimental/filesystem>
#include <iterator>
#include <fstream>

// copy names of regular files in the directory (as std::string) to the output iterator
template < typename STRING, typename OUTPUT_ITERATOR >
OUTPUT_ITERATOR copy_file_names( STRING path_to_dir, OUTPUT_ITERATOR dest )
{
    namespace fs = std::experimental::filesystem ;

    // note: this iterator does not recurse into subfirectories. to get names of files in   
    //       this directory and its subdirectories, change the type of the iterator to
    //       fs::recursive_directory_iterator
    fs::directory_iterator iter(path_to_dir), end ;
    
    for( ; iter != end ; ++iter ) if( fs::is_regular_file(*iter) ) *dest = iter->path().string() ;
    // note: this copies the name only if the file is a regular files 
    //       comment out /* if( fs::is_regular_file(*iter) ) */ to copy the names of all files
    // note: this copies the file names as std::string. to copy them as std::wstring 
    //       change *dest = iter->path().string() ; to *dest = iter->path().wstring() ; 
        
    return dest ;
}

int main()
{
    const std::wstring path_wstr = L"C:/Windows/" ; // adjust as required
    const std::string text_file_name = "file_list.txt" ; // adjust as required

    {
        std::ofstream file(text_file_name) ;
        copy_file_names( path_wstr, std::ostream_iterator<std::string>( file, "\n" ) );
    }

    #ifndef NDEBUG // debugging support
        std::cout << "file '" << text_file_name << "' contains:\n--------------------\n"
                  << std::ifstream(text_file_name).rdbuf() ;
    #endif
}

http://rextester.com/ZOOB65132
@JLBorges : thank you , but i am working with DevC++ ,
i cant find the library filesystem.h ,
besides it is related to c++17 standard !
in my code , when i use short path it lists all files , but when i do not use short path i can not get the full name of the russian named file , why ?
Topic archived. No new replies allowed.