Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I'm trying to understand how can I make a function that gives me all the files name in a specific directory and iterate for each file to print a message, how can I accomplish this function what library in c ++ should I use?

What I have tried:

I tried to use boost library but I failed installing it
Posted
Updated 7-Jan-21 1:04am

 
Share this answer
 
hi, I found out some code or a way to accomplish my goal, I understand the code 100% but the first problem is when I run the code it throws an Error with the code (123) like:-

FindFirstFile Failed(123)


the second problem is I want to iterate for each file to call a function and within the function, there is an argument this argument is the path

here is the code::-

C++
<pre>static void listfiles() {

	WIN32_FIND_DATA FindFileData;
	HANDLE hFind = FindFirstFile("C:\\Users\MohamedAlzurghni\source\repos\FileCrypter\Debug", &FindFileData);
	if (hFind == INVALID_HANDLE_VALUE) {
		printf("FindFirstFile failed (%d)\n", GetLastError());
		return;

	}

	do {
		if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
			printf(TEXT("  %s   <DIR>\n"), FindFileData.cFileName);
		else
			printf(TEXT("  %s \n"), FindFileData.cFileName);
	} while (FindNextFile(hFind, &FindFileData) != 0);

	FindClose(hFind);
}

[Update] I tried to Fix the Problem and i wrote this:-

C++
WIN32_FIND_DATA data;
	HANDLE hFind = FindFirstFile("C:\\Users\MohamedAlzurghni\source\repos\ProjectC++\*", &data);      // DIRECTORY

	if (hFind != INVALID_HANDLE_VALUE) {
		do {
			std::cout << data.cFileName << std::endl;
		} while (FindNextFile(hFind, &data));
		FindClose(hFind);
	}

but there is no output.
 
Share this answer
 
v2
Comments
Richard Deeming 7-Jan-21 7:16am    
How is this meant to be a "solution" to your question?
MohammedZr 7-Jan-21 8:11am    
can you please check the question again I think this is the solution for `the question`
Richard Deeming 7-Jan-21 8:44am    
So the code works and solves your question, and the list of problems in your solution don't exist?
Richard MacCutchan 7-Jan-21 8:20am    
"but there is no output."
Most likely because your call to FindFirstFile failed, but you ignored the error. If a systme call fails then you need to investigate why, and correct your code. Just ignoring it is not a good way to create a working program.
Richard MacCutchan 7-Jan-21 8:22am    
In both cases above your path strings are invalid because they contain unescaped backslash characters, which you can clearly see.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900