1

I run this program to output the contents of the current working directory. I get the wrong output. Program is listed below with the output I get posted after it.

code

#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>



int main ()
{
DIR *gdir = NULL;
gdir = opendir(".");

struct dirent *hdir = NULL;

//hdir.d_ino = 123;
//hdir->d_name[256] = NULL;

if (gdir == NULL)
{
    printf ("Could not open directory.");
    exit (3);
}

while ( hdir = readdir(gdir) )
{       
    if (hdir == NULL)
    {
        printf ("Could not read from directory.");
        //printf ("%d\n", i);
        exit (3);
        //printf ("hdir.d_ino: %ld", hdir.d_ino);

    }
    else
    {
        printf ("Name:  %25s\n", hdir->d_name);
    }

    //i++;
}
//printf ("%d\n", i);

closedir(gdir);
//printf ("hdir.d_reclen: %u", hdir.d_reclen);
system ("PAUSE");

return 0;
}

output

Name:
Name:
Name:
Name:
Name:                    ry.c

Notice the ry.c file with the .c file extension. That is one of the files in the current working directory. I do not understand why only one truncated file name is outputted where the other files are not.

platform details

I have the current mingw installation, compiling with gcc, on windows 8. I am using Windows command line to compile.

alk
  • 69,737
  • 10
  • 105
  • 255
  • also post the file names in that directory ( `dir` ) – amdixon Sep 12 '15 at 00:54
  • Does the program work for you amdixon? Do the contents of your directory output correctly? –  Sep 12 '15 at 01:06
  • Besides `PAUSE` most likely is unknow/undefined the code looks okay. – alk Sep 12 '15 at 09:03
  • @alk can you explain why these is not output and the ry.c is truncated? –  Sep 13 '15 at 22:53
  • Are those file's name containing any characters different from ASCII? – alk Sep 14 '15 at 15:35
  • @alk No. The file's names are ASCII values. Should I check for ASCII values by inserting a C function to determine if they are ASCII values in the while loop? –  Sep 19 '15 at 22:09
  • I output the d_ino value printf ("%d\n", hdir->d_ino); and the value comes back as a 0. The d_ino is the file serial number for struct dirent. Why is it zero? Could this be related to the incorrect output from my program? –  Sep 19 '15 at 22:42
  • I am convinced now it is a bad dirent.h file included with mingw. I do not know how to resolve this. Any suggestions from anyone? –  Sep 21 '15 at 00:14
  • I found this as a possible solution: http://stackoverflow.com/questions/27554325/readdir-32-64-compatibility-issues –  Sep 24 '15 at 13:38

0 Answers0