# Program

**URL:** <https://community.unix.com/t/program/320941>\
**Category:** Programming\
**Created:** [December 7, 2012, 1:02am UTC](https://community.unix.com/t/program/320941 "2012-12-07T01:02:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shellwe](https://community.unix.com/letter_avatar/shellwe/32/6_449c533f586939b6e5f246632a46421f.png) [@shellwe](https://community.unix.com/u/shellwe)\
**Post date:** [December 7, 2012, 1:02am UTC](https://community.unix.com/t/program/320941/1 "2012-12-07T01:02:57Z")

</div>

my this program is working when the argument is not provided why its not working when i m providing argument?

code :  
#include\<iostream\>  
#include \<dirent.h\>  
using namespace std;  
int main(int agrc ,char _val[])  
{  
DIR_dir;  
struct dirent \* abcd;  
if(!val[1])  
{  
/_cout \<\< "Invalid Execution Style: Use \<Program Name\> \<Folder location\>\n";_/  
dir = opendir(".");  
if(dir !=NULL)  
{  
while ((abcd=readdir (dir))!=NULL)  
{  
if( abcd-\>d\_name[0] != '.' )  
printf ("[%s]\n", abcd-\>d\_name);

```
                    \}
                    closedir\(dir\);

                    exit\(0\);
            \}
            dir = opendir\(val[1]\);
            if\(dir !=NULL\)
            \{
                    while \(\(abcd=readdir \(dir\)\)!=NULL\)
                    \{
                            if\( abcd-&gt;d_name[0] != '.' \)
                                    printf \("[%s]\\n", abcd-&gt;d_name\);
                    \}
                    closedir\(dir\);
                    return 0;
            \}
    \}

```

}

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [December 7, 2012, 1:29am UTC](https://community.unix.com/t/program/320941/2 "2012-12-07T01:29:33Z")

</div>

> [@shellwe](#):
>
> my this program is working when the argument is not provided why its not working when i m providing argument?
> 
> ```plaintext
> #include<iostream>
> #include <dirent.h>
> using namespace std;
> int main(int agrc ,char *val[])
> {
> DIR*dir;
> struct dirent * abcd;
> if(!val[1])
> {
> /*cout << "Invalid Execution Style: Use <Program Name> <Folder location>\n";*/
> dir = opendir(".");
> if(dir !=NULL)
> {
> while ((abcd=readdir (dir))!=NULL)
> {
> if( abcd->d_name[0] != '.' )
> printf ("[%s]\n", abcd->d_name);
> 
> }
> closedir(dir);
> 
> exit(0);
> }
> dir = opendir(val[1]);
> if(dir !=NULL)
> {
> while ((abcd=readdir (dir))!=NULL)
> {
> if( abcd->d_name[0] != '.' )
> printf ("[%s]\n", abcd->d_name);
> }
> closedir(dir);
> return 0;
> }
> }
> }
> 
> ```

Note the line of text I marked in red in your posting above. My guess would be that you never get to the part where you try to read the directory named by the 1st operand. Try rebuilding and rerunning your code after removing that line.

---

<div class="post-metadata">

**Author:** ![shellwe](https://community.unix.com/letter_avatar/shellwe/32/6_449c533f586939b6e5f246632a46421f.png) [@shellwe](https://community.unix.com/u/shellwe)\
**Post date:** [December 7, 2012, 1:37am UTC](https://community.unix.com/t/program/320941/3 "2012-12-07T01:37:57Z")

</div>

i removed exit (0)...but its still the same

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [December 7, 2012, 1:59am UTC](https://community.unix.com/t/program/320941/4 "2012-12-07T01:59:14Z")

</div>

```nohighlight
                dir = opendir(val[1]);
                if(dir !=NULL)
                {
                        while ((abcd=readdir (dir))!=NULL)
                        {
                                if( abcd->d_name[0] != '.' )
                                        printf ("[%s]\n", abcd->d_name);
                        }
                        closedir(dir);
                        return 0;
                } else {
                        printf("opendir(%s) failed\n", val[1]);
                        return 1;
                }
        }
}

```

So try adding the code in red above and see if it does anything.
