Note that there are some explanatory texts on larger screens.

plurals
  1. POC stat() ignoring files
    primarykey
    data
    text
    <p>I'm having an issue with the stat function in C. My application must list all files in two directories (2nd directory not implemented yet). When dir1 is set to "." for current directory it lists all files. If I change it to the required directory it will only list one file.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;dirent.h&gt; #include &lt;sys/stat.h&gt; #include &lt;sys/types.h&gt; main () { DIR *dir1; DIR *dir2; dir1 = opendir ("/home/tom/Documents/Uni/Dropbox/OS/C/1/"); dir2 = opendir ("/home/tom/Documents/Uni/Dropbox/OS/C/2/"); struct dirent *ent; struct stat fileStat; if (dir1 != NULL) { /* while there are files to read in the directory */ while ((ent = readdir (dir1)) != NULL) { /*printf ("In 1\n"); &lt;--debugging--&gt; */ if (stat(ent-&gt;d_name,&amp;fileStat) == 0) { /* ignores . and .. and hidden files */ /* printf ("In 2\n"); &lt;--debugging--&gt; */ if(ent-&gt;d_name[0] != '.') { /* printf ("In 3\n"); &lt;--debugging--&gt; */ printf ("\n"); printf ("File: %s\n", ent-&gt;d_name); printf ("File size: %d\n", fileStat.st_size); printf ("-----------------------------------\n"); } } } /* close the 1st directory */ closedir (dir1); /* close the 2nd directory */ closedir (dir2); } else { /* prints an error if the directory can not be opened */ perror (""); } } </code></pre> <p>The result of running the program is below:</p> <pre><code>tom@x60deb:~/Documents/Uni/Dropbox/OS/C$ ./ffffuuuuuu File: ffffuuuuuu.c File size: 1045 ----------------------------------- </code></pre> <p>This is the result of ls in the directory it is reading:</p> <pre><code>tom@x60deb:~/Documents/Uni/Dropbox/OS/C/1$ ls -l total 36 -rw-r--r-- 1 tom tom 356 Dec 12 23:36 cwTest2.c -rw-r--r-- 1 tom tom 322 Dec 12 23:36 cwTest.c -rw-r--r-- 1 tom tom 627 Dec 12 23:36 ffffuuuuuu.c -rw-r--r-- 1 tom tom 6 Dec 12 23:32 file -rw-r--r-- 1 tom tom 6 Dec 12 23:32 file2 -rw-r--r-- 1 tom tom 6 Dec 12 23:45 file2.file -rw-r--r-- 1 tom tom 15 Dec 12 23:33 file3 -rw-r--r-- 1 tom tom 15 Dec 12 23:45 file3.file -rw-r--r-- 1 tom tom 6 Dec 12 23:45 file.file </code></pre> <p>Thanks in advance, Tom.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload