Problem Using opendir

I have problems opening a file within a directory.

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <dirent.h>
#include <string>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 

#include <getopt.h>
#include <stdarg.h>
#include <fstream>
using namespace std;

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

/* This is just a sample code, modify it to meet your need */
int main(int c, char* v[])
{
	int len;
    struct dirent *pDirent;
    DIR *pDir;

    if (c < 2) {
        printf ("Usage: testprog <dirname>\n");
        return 1;
    }
    pDir = opendir (v[1]);
    if (pDir == NULL) {
        printf ("Cannot open directory '%s'\n", v[1]);
        return 1;
    }

   /*while ((pDirent = readdir(pDir)) != NULL) {
        printf ("[%s]\n", pDirent->d_name);
    }*/
  
//---------------------------
    DIR* FD;
   // struct dirent* in_file;
    FILE    *common_file;
    FILE    *entry_file;
    char    buffer[BUFSIZ];

    /* Scanning the in directory */

    while ((pDirent = readdir(pDir))) 
    {
    	printf ("[%s]\n", pDirent->d_name);
    	
    	if (!strcmp (pDirent->d_name, "."))
            continue;
        if (!strcmp (pDirent->d_name, ".."))    
            continue;

        // Open directory entry file for common operation 
        // TODO : change permissions to meet your need! 

        entry_file = fopen(pDirent->d_name, "r");
        if (entry_file == NULL)
        {
            fprintf(stderr, "Error : Failed to open entry file - %s\n", strerror(errno));
          //  fclose(common_file);
            return 1;
        }

        // Doing some stuff with entry_file : 
        // For example use fgets 
        while (fgets(buffer, BUFSIZ, entry_file) != NULL)
        {
            // Use fprintf or fwrite to write some stuff into common_file
        }

        // When you finish with the file, close it 
        fclose(entry_file);
    }

    // Don't forget to close common file before leaving 
   // fclose(common_file);


    closedir (pDir);
    return 0;
}


I appreciate your help.
Error Message:

$ ./dirsearch ~/Documents/College/textfiles/
[..]
[yomama.txt]
Error : Failed to open entry file - No such file or directory


Here are my current file permissions:

textfiles$ ls -l
total 8
-rw-rw-rw- 1 jav jav  7 Apr 26 13:14 moretext.txt
-rw-rw-rw- 1 jav jav 10 Apr 26 12:38 yomama.txt
Last edited on
d_name only contain the file name, not the full path.
Could you suggest a way to pass the full path of .txt files by just passing a whole directory path to char* v[]?
I added this code and now I made some progress.
1
2
3
4
5
6
7
 entry_file = fopen( strcat(v[1],pDirent->d_name), "r");
        if (entry_file == NULL)
        {
            fprintf(stderr, "Error : Failed to open entry file - %s\n", strerror(errno));
          //  fclose(common_file);
            return 1;
        }


I still get this error:

$ ./dirsearch ~/Documents/College/textfiles/
[..]
[yomama.txt]
[.]
[moretext.txt]
Error : Failed to open entry file - No such file or directory

Last edited on
You are using strcat to add each name at the end of the string, but the old names will still be there.
~/Documents/College/textfiles/
~/Documents/College/textfiles/yomama.txt
~/Documents/College/textfiles/yomama.txtmoretext.txt

And it is probably not a good idea to write to v[1]. Not sure if you are allowed to write to it, but the array might not be big enough to add more characters to the string anyway.
Last edited on
I added some debugging code. Notice that the rest of the code remains the same.
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
46
47
48
49
50
51
52
53
54
55
//---------------------------
    DIR* FD;
   // struct dirent* in_file;
    FILE    *common_file;
    FILE    *entry_file;
    char    buffer[BUFSIZ];

    /* Scanning the in directory */
   // printf("whats in v[] %s\n",v[1]);
    while ((pDirent = readdir(pDir))!=NULL ) 
    {
    	printf ("[%s]\n", pDirent->d_name);
    	
    	if (!strcmp (pDirent->d_name, "."))
    	{	
    		printf("about to continue with '.'\n");
            continue;
        }
        if (!strcmp (pDirent->d_name, ".."))    
        {
        	printf("about to continue '..'\n");
            continue;
        }
        
        // Open directory entry file for common operation 
        // TODO : change permissions to meet your need! 
        entry_file = fopen( strcat(v[1],pDirent->d_name), "r");
        if (entry_file == NULL)
        {
            fprintf(stderr, "Error : Failed to open entry file - %s\n", strerror(errno));
          //  fclose(common_file);
            return 1;
        }
        else
        	printf("opened %s succesful. \n",pDirent->d_name );

        // Doing some stuff with entry_file : 
        // For example use fgets 
        while (fgets(buffer, BUFSIZ, entry_file) != NULL)
        {
            // Use fprintf or fwrite to write some stuff into common_file
        }

        // When you finish with the file, close it 
        fclose(entry_file);
        printf ("next d_name is [%s]\n", pDirent->d_name);
    }

    // Don't forget to close common file before leaving 
   // fclose(common_file);


    closedir (pDir);
    return 0;
}


Here's my output.

$ ./dirsearch ~/Documents/College/textfiles/
[..]
about to continue '..'
[yomama.txt]
opened yomama.txt succesful. 
next d_name is [yomama.txt]
[.]
about to continue with '.'
[moretext.txt]
Error : Failed to open entry file - No such file or directory


Thank you very much, Peter.

Could you think about a more efficient way to implement my code?
Here:
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//---------------------------
    DIR* FD;
   // struct dirent* in_file;
    FILE    *common_file;
    FILE    *entry_file;
    char    buffer[BUFSIZ];

	char str[sizeof v]; //v is the second argument of main
	strcpy (str,v[1]);
	
	/* Scanning the in directory */
   // printf("whats in v[] %s\n",v[1]);
    while ((pDirent = readdir(pDir))!=NULL ) 
    {
    	printf ("[%s]\n", pDirent->d_name);
    	
    	if (!strcmp (pDirent->d_name, "."))
    	{	
    		printf("about to continue with '.'\n");
            continue;
        }
        if (!strcmp (pDirent->d_name, ".."))    
        {
        	printf("about to continue '..'\n");
            continue;
        }
        
        // Open directory entry file for common operation 
        // TODO : change permissions to meet your need! 
        entry_file = fopen( strcat(str,pDirent->d_name), "r");
        if (entry_file == NULL)
        {
            fprintf(stderr, "Error : Failed to open entry file - %s\n", strerror(errno));
          //  fclose(common_file);
            return 1;
        }
        else
        	printf("opened %s succesful. \n",pDirent->d_name );

        // Doing some stuff with entry_file : 
        // For example use fgets 
        while (fgets(buffer, BUFSIZ, entry_file) != NULL)
        {
            // Use fprintf or fwrite to write some stuff into common_file
        }

        // When you finish with the file, close it 
        fclose(entry_file);
        printf ("next d_name is [%s]\n", pDirent->d_name);
        memset(str, 0, sizeof str);
        strcpy(str,v[1]);
    }

    // Don't forget to close common file before leaving 
   // fclose(common_file);


    closedir (pDir);
    return 0;
}
Last edited on
Topic archived. No new replies allowed.