Convert CString to DIR variable

Mar 22, 2017 at 2:31pm
Hi guys.

I have been doing well lately but have ran into a snag.

I am trying to figure out how to convert a CString to a Directory (DIR) variable.
I can not seem to find a way to do this.

The DIR variable of course is from the dirent.h header.

Any ideas? Thank you in advance.
Last edited on Mar 22, 2017 at 2:31pm
Mar 22, 2017 at 3:19pm
Hello Kmpck,

As I read the header file "DIR" is the name of a struct not a variable. Could you show me what you are trying to do?

Andy
Mar 22, 2017 at 3:35pm
I may be way off base with a few things I say, that is not intentional. The area that I am venturing in to with this is completely new for me.

I have dirent.h added as a header

I have created the following:
CString tempString;
DIR *dir;

If I am not mistaken, dirent.h has to do with directory structures.
DIR *dir is what I am trying to assign a directory structure to. For example “C:\users\user\MyDocuments”
tempString is a CString that currently has the directory that the user chooses stored in it. I am trying to get the value of tempString and assign it to dir.

Does that make sense?
Mar 22, 2017 at 3:39pm
What I am trying to do as a whole is make a list of all the files and folders in a directory. All the research I have done so far points to using dirent.h to do this. This is a file parsing app I am making. I have done work before like this with directories where I already knew all the names of the files and folders in advance but never where I will not have that information and need my app to look at the folder and gather what is in it on its own.
Last edited on Mar 22, 2017 at 3:42pm
Mar 22, 2017 at 3:43pm
You need to call opendir(...) in order to get an object of the type DIR.

See:

http://pubs.opengroup.org/onlinepubs/009695399/functions/opendir.html

opendir(...) expects a c string that you can obtain from the CString with i.e. GetBuffer(...):

https://msdn.microsoft.com/en-us/library/aa314880(v=vs.60).aspx
Mar 22, 2017 at 4:19pm
@coder777

Correct, I came to that conclusion a while back today. The problem is that it is not working and I can not see why. I KNOW that this will be something small and simple. Here is my code:

Look for this line:
"DIR *dir = opendir(Var_Dir::GetBuffer()); //Errors out no matter what I try here. I know it is something simpe"




void ParseIt(CString Var_Dir, CString Out_Dir)
{
AfxMessageBox(_T("Testing again 3")); //For testing
AfxMessageBox(_T("Open directory") + Var_Dir); //For testing
AfxMessageBox(_T("Out directory") + Out_Dir); //For testing

static ifstream fileIN;
static ofstream fileOUT;
fileOUT.open("MyOutFile.txt");

//Var_Dir is the CStruct with the directory value that we need to examine
CT2A back2string(Var_Dir);


string tempStr = back2string;
AfxMessageBox(_T("Testing yet again: ") + Var_Dir); //For testing



////////////////////
////////////////////
//And here is where I am stuck. I know this line is not right
DIR *dir = opendir(Var_Dir::GetBuffer()); //Errors out no matter what I try here. I know it is something simpe
////////////////////
////////////////////


struct dirent *ent;

//CString csTemp;

if (dir != NULL)
{
AfxMessageBox(_T("Made it here.")); //For testing purposes. This confirms that dir received the value.
while ((ent = readdir(dir)) != NULL)
{
fileOUT << ent << "\n";
printf("%s\n", ent->d_name);
}
}
else
{
AfxMessageBox(_T("Not working.")); //For testing purposes.
}

fileOUT.close();
fileIN.close();
}
Mar 22, 2017 at 4:54pm
Hello Kmpck,

A few months ago when I went looking for "dirent.h" I found these two sites. I do not remember which site I downloaded from first, but "GitHub" looks most familiar, but the readme.md link will direct you to "softagalleria.net".

https://github.com/tronkko/dirent
http://softagalleria.net/dirent.php

With what I found this is the code I put together. It is condensed down from what I used, but I think it should work, speaking more main than the function. The only parts of the function I changed was to comment out what I did not need.

Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <dirent.h>

#define PATH "..\\..\\" // Or any path you need.

int main()
{
	char buff[MAX_PATH]{ '\0' };  //  For ShowDir()

	const std::string fileType{ ".cpp" };  //  File Type to List.
	const char *directory{ PATH };  //  Directory to List.

	list_directory(directory, fileType);

	//  You may want a pause here.
	return 0;
}


ListDirectory.cpp
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>


/*
* List files and directories within a directory.
*/

void list_directory(const char *dirname, std::string fileType)  //  Looks like it needs to be a char *.
{
	DIR *dir;
	struct dirent *ent;
	std::size_t pos;

	//CLS;

	/* Open directory stream */
	dir = opendir(dirname);
	if (dir != NULL)
	{

		/* Print all files and directories within the directory */
		while ((ent = readdir(dir)) != NULL)
		{
			switch (ent->d_type)
			{
			case DT_REG:  //  Regular file name.
						  //printf(" File %s\n", ent->d_name);
				fileNames.push_back(ent->d_name);
				break;

				case DT_DIR:  // Dirctory name.
					printf(" Dir %s/\n", ent->d_name);
					break;

				case DT_LNK:  //  Not sure.
					printf(" %s@\n", ent->d_name);
					break;

				default: //  Will include ./ and ../
					printf(" %s*\n", ent->d_name);
			}  //  End switch
		}  //  End while

		   printf("\n");
		std::cout << "\n";

		closedir(dir);

		for (size_t lp = 0; lp < fileNames.size(); lp++)
		{
			pos = fileNames[lp].find(".");
			//std::cout << fileNames[lp].substr(pos) << std::endl;

			if (fileNames[lp].substr(pos) == fileType)
				//std::cout <</* "\n Poosition " << lp << */"\n " << fileNames[lp] << std::endl;
				std::cout << " " << fileNames[lp] << std::endl;
		}

	}  //  End if
	else
	{
		/* Could not open directory */
		printf("Cannot open directory %s\n", dirname);
		std::this_thread::sleep_for(std::chrono::seconds(5));
		exit(EXIT_FAILURE);
	}  //  End else
}  //  End Function 


Hope this helps,

Andy
Topic archived. No new replies allowed.