-4

Had to do some legacy coding in Turbo C++ for school project, when I ran into a problem using dirent.h

#include <dirent.h>
#include <stdio.h>
#include <iostream.h>

void ls(char *searchstring)
{
 DIR *directory_list;
 struct dirent *dentry;
 directory_list = opendir (searchstring);
 if (directory_list != NULL)
 {
  while ((dentry = readdir (directory_list)) != NULL)
  {
   cout << dentry->d_name << endl;
  }
 }
  closedir (directory_list);
}

void main()
{
 char searchstring[128];
 gets(searchstring);
 cout << "list of files under " << searchstring;
 ls(searchstring);
}

This code should ideally list all file and folder names in a directory. But I get this:

But I get this It seems that the d_name field doesn't seem to store file names larger than 9 characters(including null) and instead truncates it with a '~' and a number.

I read something about NAME_MAX bound length for string in dirent structure, but I can't find anymore information about it anywhere on the web.

I want the full file and folder names instead, and sorry I am bound to use the ancient Turbo C++.

Anubhabchak
  • 73
  • 1
  • 2
  • 9

1 Answers1

1

It seems that the d_name field doesn't seem to store file names larger than 9 characters(including null) and instead truncates it with a '~' and a number.

Yes, that's how the windows filesystem entries handle backward compatibility for long filenames that came up with Windows versions of Windows95 and later.

The Turbo C++ compiler uses the older standards that allowed to have filenames with a maximum of 8 characters, a dot (.) and a max 3 characters extension.

I want the full file and folder names instead, and sorry I am bound to use the ancient Turbo C++.

If you are bound to the features supported by Turbo C++, there's no way out using the native functions provided with the compiler.

You could try to write the filesystem interface at low level yourself, but it is questionable if that's worth the efforts.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Nope it's not worth the effort, atleast for me. I'll probably keep using those short names for index.(I was making a searchable file index here) or I'll just find another project to do. Thanks for your answer. :) – Anubhabchak Dec 02 '16 at 06:55