logoISU  

CS 456 - Systems Programming

Spring 2024

Displaying ./code/feb15/dir5.c

// open and read the current directory

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/types.h>

int main(int argc, char **argv){

	DIR *mydir; //defines a pointer to a directory
	struct dirent *file; //accessing the dirent struct, defined in dirent.h

	mydir = opendir(argv[1]); //opens specified directory

	// then run through directory, printing it's contents
	while((file = readdir(mydir)) != NULL){
		printf("%s\n", file->d_name);
	}

	//close the directory
	closedir(mydir);

	return 0;

}