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 :
#include<iostream>
#include<conio.h>
#include<fstream>
#include<stdio.h>
#include<stdlib.h>
#include <windows.h>
#include <string>
#include <cstdlib>
int main (int argc, constchar * argv[])
{
usingnamespace 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;
}
#include<iostream>
#include<conio.h>
#include<fstream>
#include<stdio.h>
#include<stdlib.h>
#include <windows.h>
#include <string>
#include <cstdlib>
int main (int argc, constchar * argv[])
{
usingnamespace 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;
}
#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
}
@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 ?