Consider a standalone example wherein I query for all the names in the directory with a wild card:
#include <Windows.h>
#include <fstream>
void add_file(const std::string &path)
{
std::ofstream ofs(path,std::ofstream::out);
ofs.close();
}
void foo(const std::wstring& szDir)
{
std::cout << "f1 : FindFirstFileW\n";
WIN32_FIND_DATAW ffd;
HANDLE hFind = INVALID_HANDLE_VALUE;
hFind = FindFirstFileW(szDir.c_str(), &ffd);
if (INVALID_HANDLE_VALUE == hFind)
{
std::cout << "Error in FindFirstFileW : " << GetLastError() << std::endl;
return;
}
// List all the files in the directory with some info about them.
do
{
std::wcout <<"Long file name " << " " << ffd.cFileName << std::endl;
std::wcout <<"Short file name " << " " << ffd.cAlternateFileName << std::endl;
}
while (FindNextFileW(hFind, &ffd) != 0);
FindClose(hFind);
}
int main()
{
const char odd_filename[] = {static_cast<char>(0xC4U), '.', 't', 'x', 't', 0};
add_file("C:\\mydir1\\777.Txt");
add_file(std::string("C:\\mydir1\\") + std::string(odd_filename));
foo(L"C:\\mydir1\\7*");
return 0;
}
This gives me output as below
f1 : FindFirstFileW
Long file name 777.Txt
Short file name
Long file name ─.txt
Short file name 7F7E~1.TXT
Why does FindFirstFileW give back the second file name Ä.txt as a match ?
The wildcard match is applied to both long and short file names. The second file has a short name of 7F7E~1.TXT and so matches 7*.
The documentation covers this like so:
The following list identifies some other search characteristics:
The search is performed strictly on the name of the file, not on any attributes such as a date or a file type (for other options, see
FindFirstFileEx).
The search includes the long and short file names.
An attempt to open a search with a trailing backslash always fails.
Passing an invalid string, NULL, or empty string for the lpFileName parameter is not a valid use of this function. Results in this case
are undefined.
The second bullet point is the pertinent one.
Related
I am trying to create a program which copy itself while it is running to new location without keeping the orignal file location . once it is copied I got the file without extension , but how can I overcome this ?
int _tmain(int argc, _TCHAR* argv[])
{
TCHAR szFilepath[MAX_PATH];
TCHAR szFilename[MAX_PATH];
TCHAR szDestpath[MAX_PATH];
/* Get the current executable's full path */
GetModuleFileName(NULL, szFilepath, MAX_PATH);
std::wcout << "filepath: " << szFilepath << std::endl;
/* Extract just the name */
GetFileTitle(szFilepath, szFilename, MAX_PATH);
std::wcout << "filename: " << szFilename << std::endl;
//Set the destination folder path
_tcscpy(szDestpath, L"D:\\");
//Set the destination file path
_tcscat(szDestpath, szFilename);
std::wcout << "dest path: " << szDestpath << std::endl;
// copys the file of your '.exe'
if (!CopyFile(szFilepath, szDestpath, FALSE)) {
std::cout << "couldnt copy the file";
}
else {
std::cout << "copied";
}
return 0;
}
Per the documentation for GetFileTitle():
GetFileTitle returns the string that the system would use to display the file name to the user. The display name includes an extension only if that is the user's preference for displaying file names. This means that the returned string may not accurately identify the file if it is used in calls to file system functions.
You should be using a more suitable function to get the actual filename, such as PathFindFileName():
#include <windows.h>
#include <shlwapi.h>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
WCHAR szFilepath[MAX_PATH];
LPWSTR lpszFilename;
WCHAR szDestpath[MAX_PATH];
/* Get the current executable's full path */
GetModuleFileNameW(NULL, szFilepath, MAX_PATH);
std::wcout << L"filepath: " << szFilepath << std::endl;
/* Extract just the name */
lpszFilename = PathFindFileNameW(szFilepath);
std::wcout << L"filename: " << lpszFilename << std::endl;
/* Set the destination folder path and file name */
PathCombineW(szDestpath, L"D:\\", lpszFilename);
std::wcout << L"dest path: " << szDestpath << std::endl;
// copys the file of your '.exe'
if (!CopyFileW(szFilepath, szDestpath, FALSE)) {
std::wcout << L"couldnt copy the file";
}
else {
std::wcout << L"copied";
}
return 0;
}
Or, you could simply parse the filename yourself using normal C++ string operations, eg:
#include <windows.h>
#include <iostream>
#include <string>
int _tmain(int argc, _TCHAR* argv[])
{
WCHAR szFilepath[MAX_PATH];
std::wstring wFilepath;
std::wstring wFilename;
std::wstring wDestpath;
/* Get the current executable's full path */
wFilepath = std::wstring(szFilepath, GetModuleFileNameW(NULL, szFilepath, MAX_PATH));
std::wcout << L"filepath: " << wFilepath << std::endl;
/* Extract just the name */
wFilename = wFilepath.substr(wFilepath.find_last_of(L"\\/")+1);
std::wcout << L"filename: " << wFilename << std::endl;
/* Set the destination folder path and file name */
wDestpath = L"D:\\" + wFilename;
std::wcout << L"dest path: " << wDestpath << std::endl;
// copys the file of your '.exe'
if (!CopyFileW(wFilepath.c_str(), wDestpath.c_str(), FALSE)) {
std::wcout << L"couldnt copy the file";
}
else {
std::wcout << L"copied";
}
return 0;
}
I am trying to store the name of all txt files in a directory in a string and print them out. I need to count the number of txt files in the directory and then print the names. The part of counting is working, but I can't seem to get the name working. I have found some examples but they don't work in visual studio which is what I'm using.
Here is my code.
int main() {
bool x = true;
int i = 0;
wchar_t* file = L"../Menu/Circuitos/*.txt";
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
hFind = FindFirstFile(file, &FindFileData);
if (hFind != INVALID_HANDLE_VALUE) {
i++;
while ((x = FindNextFile(hFind, &FindFileData)) == TRUE) {
i++;
}
}
cout << "number of files " << i << endl;
return 0;
}
FindFirstFile already has the first valid handle. If you immediately call FindNextFile then the first handle is lost. The file count in your example would be wrong.
Use do-while loop istead.
Also, the handle obtained from FindFirstFile must be closed with FindClose
HANDLE hFind;
hFind = FindFirstFile(file, &FindFileData);
if (hFind != INVALID_HANDLE_VALUE)
{
do {
wcout << FindFileData.cFileName << "\n";
i++;
} while (FindNextFile(hFind, &FindFileData));
FindClose(hFind);
}
cout << "number of files " << i << endl;
Use std::vector and std::wstring to store the items
#include <string>
#include <vector>
...
std::vector<std::wstring> vs;
HANDLE hFind;
hFind = FindFirstFile(file, &FindFileData);
if (hFind != INVALID_HANDLE_VALUE)
{
do {
vs.push_back(FindFileData.cFileName);
} while (FindNextFile(hFind, &FindFileData));
FindClose(hFind);
}
std::cout << "count:" << vs.size() << "\n";
for (auto item : vs)
std::wcout << item << "\n";
For some older compilers auto may not be available, use this instead
for (int i = 0; i < vs.size(); i++)
std::wcout << vs[i] << "\n";
Note, Windows API works with c-strings. In many cases you have to use .c_str() to obtain character array. For example:
if (vs.size())
{
std::wstring str = vs[0];
MessageBox(0, str.c_str(), 0, 0);
}
Here is a portable version using the new ISO Standard Filesystem Library TS (technical specification) for those with compilers that support it:
#include <vector>
#include <iostream>
#include <algorithm>
#include <experimental/filesystem>
// for readability
namespace fs = std::experimental::filesystem;
/**
* Function object to test directory entries
* for a specific file extension.
*/
struct file_extension_is
{
std::string ext;
file_extension_is(std::string const& ext): ext(ext) {}
bool operator()(fs::directory_entry const& entry) const
{
return entry.path().extension() == ext;
}
};
int main(int, char* argv[])
{
try
{
// directory supplied on the command line if present
// else current directory
fs::path dir = argv[1] ? argv[1] : ".";
// place to store the results
std::vector<fs::directory_entry> entries;
// copy directory entries that have file extension ".txt"
// to the results
fs::directory_iterator di(dir);
fs::directory_iterator end;
std::copy_if(di, end, std::back_inserter(entries),
file_extension_is(".txt"));
// print it all out
std::cout << "Number of files: " << entries.size() << '\n';
for(auto const& entry: entries)
std::cout << entry.path().string() << '\n';
}
catch(std::exception const& e)
{
std::cerr << e.what() << '\n';
}
catch(...)
{
std::cerr << "Unknown exception." << '\n';
}
}
I found Get DLL path at runtime but I'm not sure what to use for the localFunc variable. I tried the filename of the DLL, I tried null and some other things, but the status returned was always 'File Not Found'.
From the MSDN:
lpModuleName [in, optional]
The name of the loaded module (either a .dll or .exe file), or an address in the module (if dwFlags is GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS).
So I assume they just mean the plain file name eg, "MyControl.dll" and not the path to file, since I do not know the path.
Edit: added the actual code:
char localFunc[MAX_PATH]
sprintf_s(localFunc, 52, "MyActiveXComponent.dll");
if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR) &localFunc, &hm))
{
int ret = GetLastError();
OutFile << L"GetModuleHandle returned " << ret << std::endl;
} else {
GetModuleFileNameA(hm, path, sizeof(path));
OutFile << L"Path of dll is:" << path << L"<" << std::endl;
}
Here's what I ended up with (performed both ways)
LPCWSTR anotherFunc = L"MyActiveXComponents.dll";
HMODULE hm2 = GetModuleHandle(anotherFunc); // get the handle to the module
LPWSTR anotherPath = new WCHAR[MAX_PATH];
GetModuleFileName(hm2, anotherPath, MAX_PATH); // get the full path
OutFile << L"Path of dll is:" << anotherPath << L"<" << std::endl;
Here's the other way.
char path[MAX_PATH];
HMODULE hm = NULL;
char localFunc[MAX_PATH] = {"MyActiveXComponents.dll"};
if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, LPCSTR) &localFunc, &hm))
{
int ret = GetLastError();
OutFile << L"GetModuleHandle returned " << ret << std::endl;
} else {
GetModuleFileNameA(hm, path, sizeof(path));
OutFile << L"Path of dll is:" << path << L"<" << std::endl;
}
Thanks. I'm sure it is simple question.
Call GetModuleHandle() with the raw name like user32.dll or whatever the name of the DLL is. After you have the handle, call GetModuleFileName() to get the fully qualified name including path.
Has anyone used the FindFirstFile function to scan multiple files of the same type?
int main(int argc, char** argv){
if(argc != 3)
{
cout <<" Usage: Run [dir of images folder] [file format]" << endl;
cout <<" Example: Run \\imgs\\\\ .jpg " << endl;
return 0;
}
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
string dir = argv[1]; // store user input dir
string type = argv[2]; // store user input file type
stringstream sst;
sst << dir << "*" << type;
string folderDir = sst.str();
sst.str("");
cout << "Scanning all " << type << " files in "<< dir << endl;
cout << folderDir << endl;
/* LOADING IMAGES IN FOLDER */
I tried folderDir.c_str() instead of "\imgs\*.jpg" but I can't make it work;
hFind = FindFirstFile("\imgs\\*.jpg", &FindFileData); //images must be in .vcxproj dir
if (hFind != INVALID_HANDLE_VALUE){
int i = 0;
do {
char loc[50] = "\imgs\\"; // Obvsly, I couldn't assign argv[1] here
images.push_back(imread(strcat(loc,FindFileData.cFileName))); //pushes images into vector
img_fname[i] = FindFileData.cFileName; // stores file names into string array
cout << "Successfully loaded " << FindFileData.cFileName << endl; //prints loaded file names
i++;
}while(FindNextFile(hFind, &FindFileData));
}
Also, may I ask help in assigning string dir to char loc[50] = "\imgs\\";?
If only char loc[50] = dir; is possible...
I tried strcpy(loc, dir.c_str()); after instantiating loc but it still failed. Gave me an error (Unrecognized or unsupported array type) in unknown function
i think, it should be only one backslash there:
"imgs\*.jpg" instead of "\imgs\\*.jpg".
this works fine for me ( and gives me the filelist ):
std::vector<std::string> readdir( const char * dmask )
{
std::vector<std::string> vec;
HANDLE hFind;
WIN32_FIND_DATA FindFileData;
if ((hFind = FindFirstFile(dmask, &FindFileData)) != INVALID_HANDLE_VALUE)
{
do {
vec.push_back( FindFileData.cFileName );
} while(FindNextFile(hFind, &FindFileData));
FindClose(hFind);
}
return vec;
}
std::vector<std::string> files = readdir("imgs\*.jpg");
The following C++ code is to read a txt file and then write the numbers of chars in each line and the number of all chars in the txt file.I use MS Visual Studio 2008.But something is wrong.Only the number of all chars is input into the txt files,but the numbers of each line are not input into the txt files.Now I cannot figure it out.Could someone give me some advice?Thanks a lot!
And my another question is what should I do to insert something in the middle of the txt file?
This is my Code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream inOut("copy.txt",ios::in | ios::out | ios::app);
if (!inOut)
{
cerr << "ERROR:cannot open file!" << endl;
return -1;
}
int cnt = 0;
char ch;
inOut.seekg(0);
while(inOut.get(ch))
{
cout.put(ch);
++cnt;
if ('\n' == ch)
{
ios::pos_type mark = inOut.tellg();
if (!inOut)
{
cerr << "ERROR!" << endl;
return -1;
}
inOut << cnt;
inOut.put(' ');
inOut.seekg(mark);
}
}
inOut.clear();
inOut << cnt << endl;
cout << "[" << cnt << "]" << endl;
}
The txt file before running:
The txt file after running:
The result in command line:
I don't know fstream's very well, but I think you want to change the way you are doing this. You can think of a file as a contiguous piece of memory. Appending on the end is easy, but inserting in the middle can be problematic. In particular, if you do insert something, then your seekg might not be valid.
I would recommend three strategies:
Understand what is going on currently (try closing file before seekg and see if anything gets written by the inOut << cnt;)
Read from one file, write to a different file -- this will most likely be more efficient and less complicated than trying to modify the file in place.
Read from source file, store and modify in memory, write out modified buffer to original file. For large files, this might be less efficient than #2, but it means you don't need two files on disk.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream inOut("copy.txt",ios::in | ios::out | ios::app);
if (!inOut)
{
cerr << "ERROR:cannot open file!" << endl;
return -1;
}
int cnt = 0;
int cntline=0;
char ch;
inOut.seekg(0);
while(inOut.get(ch))
{
cout.put(ch);
//++cnt it include '\n' it should put behind of "if"
if ('\n' == ch)
{ ios::pos_type mark = inOut.tellg();
if (!inOut)
{
cerr << "ERROR!" << endl;
return -1;
}
cntline=cnt-cntline; //add it to account the number of chars of each line
inOut.seekg(0,ios::end); //here!!!!!!!!!!!!!!!!!!!!!!!!!!! add this , the resault is finally right! the reason it's down here.
inOut << cntline;
cntline=cnt; // and here! it can assure the number of the chars of each line be right!
inOut.put(' ');
inOut.seekg(mark);
}
else
{
++cnt; // it's here!
}
}
inOut.clear();
inOut << cnt << endl;
cout << "[" << cnt << "]" << endl;
}
inOut.seekg(0,ios::end)
i'am not sure it,but you can take it a reference.
you open file with "ios::app",so the VS 2008 only allows you to add text start from the end of file (it won't happened in vc 6.0).
if the file is :
it's compile by
vs 2008
when get the first '\n' the file pointer is pointing to'vs 2008'(yes,it just like the common pointer point the string ). and i debug it then find a value of the stream object named _Wrotesome .its value is false!
so i think the compiler think the file pointer point at o const sting.so you just can't write anything whis this position. so i add the code inOut.seekg(0,ios::end);.now you
can write anything you want to this text!
wish this can help you !
I don't know why do you choose std::fstream as your tool. If std::fstream is not asked or necessary, I would like to provide a example to solve your problem. Here's some limitation:
a. I read all the file into memory in one ReadFile operation. If your file is large, you may replace it with a loop.
b. I suppose your line separator is '\n'.
c. I add [line_count] before the '\n', which look like better.
here's my code.
#include "windows.h"
#include "sstream"
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hFile = ::CreateFile(L"C:\\Users\\wujian\\Desktop\\pingback - Copy.log", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE )
{
DWORD dwSize = ::GetFileSize(hFile, NULL);
if (dwSize)
{
char *pBuffer = new char[dwSize];
DWORD dwRead = 0;
::ReadFile(hFile, pBuffer, dwSize, &dwRead, NULL);
if (dwRead == dwSize)
{
std::stringstream ss;
int iPos = 0;
int iLine = 0;
while (iPos < dwSize)
{
if (pBuffer[iPos] == '\n')
{
ss << '[' << iLine << ']';
iLine = 0;
}
ss << pBuffer[iPos];
iPos ++, iLine ++;
}
ss << '[' << dwSize << ']';
::SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
DWORD dwWrite = 0;
::WriteFile(hFile, ss.str().c_str(), ss.str().length(), &dwWrite, NULL;
}
::CloseHandle(hFile);
}
}
return 0;
}