User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 375,231 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,195 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 309 | Replies: 6 | Solved
Reply
Join Date: May 2008
Location: Mumbai, India
Posts: 4
Reputation: harsh2327 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
harsh2327 harsh2327 is offline Offline
Newbie Poster

How to read complete file names???

  #1  
May 10th, 2008
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)



  1. #include <dirent.h>
  2. #include <stdio.h>
  3. #include<conio.h>
  4.  
  5. void main(void)
  6. {
  7. clrscr();
  8. DIR *d;
  9. struct dirent *dir;
  10. d = opendir(".");
  11. if (d)
  12. {
  13. while ((dir = readdir(d)) != NULL)
  14. {
  15. printf("%s\n", dir->d_name);
  16. }
  17.  
  18. closedir(d);
  19. }
  20.  
  21. getch();
  22. }



=====
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!!
Last edited by harsh2327 : May 10th, 2008 at 4:11 am.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2005
Posts: 3,058
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 20
Solved Threads: 348
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: How to read complete file names???

  #2  
May 10th, 2008
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
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Reply With Quote  
Join Date: May 2008
Location: Mumbai, India
Posts: 4
Reputation: harsh2327 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
harsh2327 harsh2327 is offline Offline
Newbie Poster

Re: How to read complete file names???

  #3  
May 10th, 2008
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??


Cheers!!
Reply With Quote  
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,556
Reputation: niek_e is a jewel in the rough niek_e is a jewel in the rough niek_e is a jewel in the rough niek_e is a jewel in the rough 
Rep Power: 8
Solved Threads: 157
niek_e's Avatar
niek_e niek_e is offline Offline
Posting Virtuoso

Re: How to read complete file names???

  #4  
May 10th, 2008
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
Last edited by niek_e : May 10th, 2008 at 8:06 am.
- On vacation -
Cheers
Reply With Quote  
Join Date: Dec 2005
Posts: 3,058
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 20
Solved Threads: 348
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: How to read complete file names???

  #5  
May 10th, 2008
> 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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Reply With Quote  
Join Date: Feb 2008
Location: Seattle
Posts: 670
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 43
jephthah's Avatar
jephthah jephthah is offline Offline
Practically a Master Poster

Re: How to read complete file names???

  #6  
May 10th, 2008
lol. dont start with Salem about void main.

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


.
Last edited by jephthah : May 10th, 2008 at 7:15 pm.
Reply With Quote  
Join Date: May 2008
Location: Mumbai, India
Posts: 4
Reputation: harsh2327 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
harsh2327 harsh2327 is offline Offline
Newbie Poster

Re: How to read complete file names???

  #7  
May 11th, 2008
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!!!!
<(^.^)>.....HM.....<(^.^)>
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 4:02 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC