Ok i am trying to have the user give a directory via the command line (./a.out /usr/bin) and then output all the files in that directory.

I don't really know how to approach this but here is what i have tried.

#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

using namespace std;

bool FileExists(char path[]) 
{
  	struct 	stat st;
  	bool 	blnReturn;
  	int 	intStat;

	if(stat(path, &st) == 0)
  	{
		blnReturn = true;
  	} 
  	else 
  	{	
  		blnReturn = false;
  	}
  
  	return(blnReturn);
}

void printData(char path[]) 
{
	struct 	stat st;
  	int 	intStat;

	if(stat(path, &st) == 0)
  	{
  		cout << st.st_dev << "\t";
		char ls[] = {"/bin/ls "};
		strcat(ls, path);
		execve(ls, NULL , NULL);
  	} 
  
}

int main(int argc, char* argv[], char *envp[])
{
	if(argc < 3)
	{
		cout << "Error with arguments" << endl;
		return 0;
	}	
	
	if(FileExists(argv[2]))
	{
		printData(argv[2], envp);
	}
	else	cout << "Directory does not exist" << endl;
	
}

Thanks fo the help.

opendir() / readdir() / closedir() would seem to be a lot simpler, if all you're after is a list of names.

or .. you could just use

dir /s/d/b usr\bin

at the command line. ;))

I was just kidding .. grep?;))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.