How to use dirent.h correctly.
Posted
by Nick
on Stack Overflow
See other posts from Stack Overflow
or by Nick
Published on 2010-06-12T18:11:01Z
Indexed on
2010/06/12
18:32 UTC
Read the original article
Hit count: 263
Hello,
I am new to C++ and I am experimenting with the dirent.h header to manipulate directory entries. The following little app compiles but pukes after you supple a directory name. Can someone give me a hint? The int quit is there to provide a while loop. I removed the loop in an attempt to isolate my problem.
thanks!
#include <iostream>
#include <dirent.h>
using namespace std;
int main()
{
char *dirname = 0;
DIR *pd = 0;
struct dirent *pdirent = 0;
int quit = 1;
cout<< "Enter a directory path to open (leave blank to quit):\n";
cin >> dirname;
if(dirname == NULL)
{
quit = 0;
}
pd = opendir(dirname);
if(pd == NULL)
{
cout << "ERROR: Please provide a valid directory path.\n";
}
return 0;
}
© Stack Overflow or respective owner