473,385 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Get file list from a directory by name in c

hi
i try to get file names listed by date or name in c. How can i do this.
there is a code: But this gives randomly file names.

Expand|Select|Wrap|Line Numbers
  1. DIR *dir;
  2. struct dirent *ent;
  3.     dir = opendir ("c:\\src\\");
  4. if (dir != NULL) {
  5.  
  6.   /* print all the files and directories within directory */
  7.   while ((ent = readdir (dir)) != NULL) {
  8.     printf ("%s\n", ent->d_name);
  9.   }
  10.   closedir (dir);
  11. } else {
  12.   /* could not open directory */
  13.   perror ("");
  14.   return EXIT_FAILURE;
  15. }
Nov 19 '12 #1
2 3177
weaknessforcats
9,208 Expert Mod 8TB
This code:
Expand|Select|Wrap|Line Numbers
  1. while ((ent = readdir (dir)) != NULL) { etc...
may be better this way:

Expand|Select|Wrap|Line Numbers
  1. while ((ent = readdir (dir)) != false) {
readdir returns a boolean of true or false. NULL is specifically a 0.
Nov 19 '12 #2
donbock
2,426 Expert 2GB
If readdir returns a boolean then that return value cannot also be used as a pointer to a dirent structure. @weaknessforcats: you're probably thinking of readdir_f.

opendir and readdir are not part of the Standard C Library. I assume you are using POSIX, but it is possible that you're using some other library that has different behavior than POSIX. Where are you getting these functions?
Nov 20 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Tony Burger | last post by:
I'm trying to drag and drop files from a file list box to a directory list box (the two controls are not related). Does anyone have an examples of this? I've got the properties setup correctly on...
6
by: o'seally | last post by:
solaris/linux admins/rookie_developers that battle with this error are probably all frustrated when it happens. i bet you're also somehow frustrated by this seemingly unsolvable error :-) ...take...
8
by: Glenn A. Harlan | last post by:
Why am I receiving the below error when calling - Path.GetTempFileName() The directory name is invalid. Description: An unhandled exception occurred during the execution of the current web...
7
by: DataSmash | last post by:
Hello, I think I've tried everything now and can't figure out how to do it. I want to read in a text list from the current directory, and for each line in the list, make a system directory for...
2
by: Alexander Keßler | last post by:
Hi, I want to programm some kind of Explorer like the Windows Explorer. And I want to know how can i get the same Icons/Symbols for the files and directories like the Windows Explorer uses. ...
1
by: Vikram | last post by:
I have suddenly started getting this errorin all my ASP.NET applications when I try to open any aspx page. Compiler Error Message: CS0016: Could not write to output file...
1
by: Steve | last post by:
Hi, I want to get a list of files in a directory, but I want to filter the list to *.vb, for example. If I use a straight forward Directory.GetFileSystemEntries(path, "*.vb") I get the...
6
by: Luca | last post by:
Hi everyone, I'm super-newbie here... so thanx in advance for any help I shoud give a "value" to an DIV's ID that change following the name of the directory where the file is. For example the...
3
by: siyaverma | last post by:
i am trying to upload csv file from user's computer to main server the code i am using is if(((isset($_GET)) && ($_GET=="yes")) ) { $typefield = $_GET; echo...
12
by: Hongyu | last post by:
Dear all: I am trying to write to a file with full directory name and file name specified (./outdir/mytestout.txt where . is the current directory) in C programming language and under Unix, but...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.