Member Avatar for harsh2327

I want to read files and folder names from directories.
I have used the following code.
But the problem is that in the output the file name gets truncated.(See the output below)

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

void main(void)
{
	clrscr();
  DIR           *d;
  struct dirent *dir;
  d = opendir(".");
  if (d)
  {
    while ((dir = readdir(d)) != NULL)
    {
      printf("%s\n", dir->d_name);
    }

    closedir(d);
  }

  getch();
}

=====
OUTPUT
=====
ABCABC~1.CPP
CURREN~1.EXE
CURREN~1.OBJ
============

The actual naes of the files are:
abcabcd.cpp
currentfolder.exe
currentfolder.obj


I would like to read the files as their original names....but I am unable to do so..

Please help!!

Salem commented: Congratulations on being one of the few to use code tags properly on the first attempt. +16
jephthah commented: lol, yeah, rock on. i thought the same thing when i saw it. +1

Recommended Answers

All 6 Replies

It's because your crusty old Turbo C (which is like a decade out of date) doesn't understand the long filenames of your nice new OS (which is it, XP or Vista?).

Get a compiler which is good for this millenium, and compatible with the win32 API. A popular choice would be.
http://www.codeblocks.org/

But there are many high quality and FREE compilers available today, there is no excuse for sticking with the old stuff.
http://www.thefreecountry.com/compilers/cpp.shtml

Oh, and void main is wrong.
http://c-faq.com/ansi/maindecl.html

Member Avatar for harsh2327

Hey Salem

Thanks a lot for your help:)


But why void main() is wrong
I mean if main() is not returning a value then why force it to return values??
:-O

Cheers!!

Actually, main DOES return a value. Never mind what you teacher told you or what Turbo thinks is normal, main returns an int. So use int main(void) and return 0; If you are switching compilers, you might want to look at the findfirst() and findnext() functions

> But why void main() is wrong
Did you bother to read the FAQ I posted?
Remember, programming at the limits of "what your current compiler will let you get away with" is no substitute for actually learning the language properly. All that implementation specific stuff will just cause you pain and suffering when you change compilers (and you will, many times).

Learning C is hard enough first time around without having to unlearn a bunch of compiler specific crap each time you change compilers. It sucked badly enough I resolved to only suffer such an experience only once. Now, I wander freely from one compiler to another and never miss a beat.

lol. dont start with Salem about void main.

not only is it his raison d'être ... but he's right


.

Member Avatar for harsh2327

Thanks guys for your help!!

Ya switching compilers is a bit of a problem but it did solve my original problem

Thanks a lot everyone!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.