フォルダー c:/ に拡張子が ".exe" のファイルが 3 つあるとします。そして、タイプ char* の 3 つのポインターを作成したいと思います。このポインターのそれぞれには、.exe ファイルのファイル名が含まれています。したがって、3 つのポインター、つまり 3 つのファイル名があります。しかし、出力は本当に私を混乱させるものです (以下を参照)。
私の実装:
#include <dirent.h>
// some code here
DIR *dir;
struct dirent *ent;
char** FileName = new char* [3]; // Creating 3 pointers of type char*
count = 0; //counting the events when .exe file detected
dir = opendir("c:/");
while ((ent = readdir (dir)) != NULL) // read directory file by file until there is nothing
{
string matchName = string(ent->d_name);
if (matchName.find(".exe") != std::string::npos) // Finding files with
//.exe extension only
{
FileName[count] = ent->d_name;
cout << "count = " << count << ": " << FileName[count] << endl;
count++; // There are 3 .exe files in the folder, so the maximum
// of the count=3 (from FileName[0] to FileName[2])
}
}
closedir (dir);
// Here I'm just checking the output
cout << "count = 0: " << FileName[0] << endl;
cout << "count = 1: " << FileName[1] << endl;
cout << "count = 2: " << FileName[2] << endl;
私の出力:
//from inside the while-loop:
count = 0: file0.exe
count = 1: file1.exe
count = 2: file2.exe
//from when I just check the output outside the loop
count = 0: // just empty for all 3 files
count = 1:
count = 2:
while ループ内にいる間は代入が期待されている (少なくとも期待されている) のに、ループの外側でポインターの同じ値をチェックしているときは、なぜ空なのですか? ありがとうございました!