error with opendir()

I'm trying to open a directory stream, but I get an error with opendir() whether or not the directory exists.

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
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>

int main(int argc, char* argv[])
{
	  struct stat buf;
	  struct passwd* user;
	  struct group* grp;
	  struct tm* tm_ptr;
	  struct dirent* dirbuf;
	  int file_desc;
	  int stat_info;
	  char* directory;
	  char* filename;
	  char file_type;
	  char timebuf[14];
	  int stat_ret;
	  time_t modtime;
	  DIR* dp;

	  if (argc !=2)
	  {
		 printf("Invalid number of arguments\n");
		 return(1);
	  }
	  //open and read directory entries */	
         if ((dp = opendir(argv[1])) == NULL);
	  {
		printf("error opening directory %s\n",argv[1]);
                perror("opendir");
		return(1);
	  }
	 

       /* ...and a bunch of other stuff that works fine */


whether or not the directory exists in the current directory, I get an "error opening directory". It's exactly the same as the code in the book unless I am missing something incredibly stupid. I added the perror to see what the actual error is, and It prints out 'SUCCESS'. The program never gets past this point at all, so I didn't bother to post 200 more lines of stuff. I've looked at online examples and they looked the same. I'm stuck until I can figure out how to open the dir, and loop through each directory entry. Thanks

edit: I'm doing this on Linux
Last edited on
if ((dp = opendir(argv[1])) == NULL); //Error - What is this semi-colon doing here?
DUH....
that's pretty embarrassing, but I guess I just looked at it too long.
Thanks so much,
Gary
Topic archived. No new replies allowed.