What would be the first parameter to this function if I want to find the first file in the directory where my program is? This directory will never be the same. Thanks.
What would be the first parameter to this function if I want to find the first file in the directory where my program is? This directory will never be the same. Thanks.
GetModuleFileName will get the complete path and filename to the current module. PathRemoveFileSpec will remove the filename and last trailing backslash. Finally we use PathIsRoot to check if it is a root directory. If it is, we must add "\\*" before passing the path to FindFirstFile.
Code:#include <shlwapi.h> TCHAR szPath[MAX_PATH + 10]; GetModuleFileName(NULL, szPath, MAX_PATH); PathRemoveFileSpec(szPath); if (PathIsRoot(szPath)) lstrcat(szPath, TEXT("\\*")); FindFirstFile(szPath, ...);
Why isn't this compiling?Code:#include <stdio.h> #include <string.h> #include "shlwapi.h" #include <windows.h> int main() { FILE *out; char path[MAX_PATH]; WIN32_FIND_DATA file; HANDLE fhandle; GetModuleFileName(NULL, path, MAX_PATH); PathRemoveFileSpec(path); if(PathIsRoot(path)) strcat(path, "\\*"); out = fopen("client_list.txt", "w"); if(out == NULL){ printf( "Error creating file\n"); exit(1); } fhandle = FindFirstFile(path, &file); if(fhandle == INVALID_HANDLE_VALUE){ printf("Invalid File Handle.\n"); exit(1); } while(FindNextFile(fhandle, &file) != 0){ if(!file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) fprintf(out, "%s\n", file.cFileName); } fclose(out); return 0; }
Errors:
linked list error LNK2019: unresolved external symbol __imp__PathIsRootA@4 referenced in function _main
c:\Documents and Settings\kao\My Documents\Visual Studio Projects\linked list\linked list\linked list.cpp(33): warning C4806: '&' : unsafe operation: no value of type 'bool' promoted to type 'int' can equal the given constant
linked list error LNK2019: unresolved external symbol __imp__PathRemoveFileSpecA@4 referenced in function _main
linked list fatal error LNK1120: 2 unresolved externals
Also, if you have any suggestions about this code, please don't hesitate to post. Thanks
Unresolved external symbol errors typically mean that you have to link with the correct library. Down the bottom of the documentation page for a Windows function is the library you need to link to. In this case it is shlwapi.lib. In MSVC, you can link to a library using a pragma (usually placed just below the includes):
The other error relates to this line:Code:#pragma comment(lib, "shlwapi.lib")
which should be:Code:if(!file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
Code:if (!(file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
try..
include windows.h and link to Kernel32.libCode://find where app is running GetCurrentDirectory(MAX_PATH, szDirectory); //or GetWorkingDirectory() //add file name or SetCurrentFolder() sprintf(szBuffer, "%s\\%s", szDirectory, szFile); //find hFindData = FindFirstFile(szBuffer, &FindData);
"Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
Friedrich Nietzsche
"I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
George Best
"If you are going through hell....keep going."
Winston Churchill
This compiles but nothing gets written to the text file (there are files in the directory). What's the reason? Thanks.Code:#include <stdio.h> #include <string.h> #include <windows.h> int main() { FILE *out; char path[MAX_PATH]; WIN32_FIND_DATA file; HANDLE fhandle; DWORD error; error = GetCurrentDirectory(MAX_PATH, path); if(error == 0){ printf( "Error\n"); exit(1); } SetCurrentDirectory(path); out = fopen("client_list.txt", "w"); if(out == NULL){ printf( "Error creating file\n"); exit(1); } fhandle = FindFirstFile(path, &file); if(fhandle == INVALID_HANDLE_VALUE){ printf("Invalid File Handle.\n"); exit(1); } while(FindNextFile(fhandle, &file) != 0){ if(!(file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) fprintf(out, "%s\n", file.cFileName); } fclose(out); return 0; }
which line fails?
what happens when you watch the code with a debugger?
test that the file can be written too, ie print something to the file before and after the filename write loop.
maybe look at the WIN32 file functions ie CreateFile()
"Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
Friedrich Nietzsche
"I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
George Best
"If you are going through hell....keep going."
Winston Churchill
You need to pass a search mask to FindFirstFile(), not just a directory. If you want all files you need to say *.*. FindFirstFile() returns the first file in the directory, you are discarding it, (although it is usually . and therefore doesn't matter).
Look at the examples in my tutorial starting here. They are C++ but if you change the headers and cout's to printf's they should work.
Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.