Opening and sorting unkown files in C++

I have a tool that creates files with the timestamp as name in a directory. The application I am creating now needs to detect and open these files in a FIFO order (that is, the oldest goes first).
I guess I could use the name or the creation attibute to do so, but how?
Can anyone help me in this one?
Thanks a lot.

Note: I can't pass the name of the file to the applicatition, I have to manage without it and still find and open the oldest one.
PrismAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gandalf32Commented:
If the amount of files isn't too large, you could read all the filenames into a memory array with FindFirstFile and FindNextFile, sort them and then open them in the correct order. If you need some code for this, I can post some.
0
nietodCommented:
Standard C++ has no provisssions for finding files in directories, or for deleting files.  These sorts of actions must be done using OS-specific code.  So what OS are you using?
0
PrismAuthor Commented:
eww... well, the code I'm writing has to work in Unix and NT. So I guess i need answers for both systems.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

nietodCommented:
That's a problem.  The same code wil not work on both.  So you will need to use "conditional compilation" to "include" or "remove" code for one OS or the other, like

#ifdef UNIX
   // put UNIX version of code here
#else
   // put NT version here.
#endif

A good way to do this, is to place this inside a procedure that does a task, and then call that proceduure whenever you need the task done.  This way, this ugly conditional code appears only is specific places in the source, like

void DeleteFile(const char *Filename)
{
#ifdef UNIX
   // put UNIX version of code here
#else
   // put NT version here.
#endif
}

   *   *   *

DeleteFile("SomeFile.Txt");
0
nietodCommented:
Now I haven't used UNIX in about 15 years, so I can't remember any of the functions you might need.  Although I believe there might be a stat() or fstat() function that returns info about file, like its date.  But that is not enough to complete your task.   I can help with the NT side, but not the UNIX part.  So I'll just recommend that you try to find "complimentary" functions between the two OSs that do a single part of your task, like delete a file, and then create functions in your code that uses conditional compilation to use the appropaite function, like in the example above.  Then you can write your code based on these functions you've written.
0
ozoCommented:
on unix, stat works with a path, and fstat works with file descriptor
but since the timestamp is the name, it doesn't sound like you'd need it.
0
nietodCommented:
"timestamp is the name" I missunderstood that.  ozo, what is available for enumerating files in a directory?
0
ozoCommented:
on a unix system, see
man readdir
0
nietodCommented:
And on Windows see FindFirstFile() and FindNextFile().
0
LucHoltkampCommented:
nietod, findfirst, findnext, unlink,  etc. is standerd stdio.h? According to my VC6 manual it is ANSI and supported on all platforms. The finddata_t structure includes a filetime.
Luc
0
nietodCommented:
Not according to my VC6 docs!  ???

First of all there is no findfirst() etc.  there is a _findfirst().  That underscore means "non-standard".   Where are you finding this?
0
KangaRooCommented:
readdir is sometimes available for Windows (at least with Borland and MingW32) so check your compiler manuals.
findfirst etc are NOT ANSI
0
nietodCommented:
readdir is not part of the windows API and is not available in the VC extensions to the standard library.

_findfirst() and _findnext() are part fo Vc's extensions to the standard library.
0
KangaRooCommented:
And so, once more it depends on the compiler you're using. I just love standards....
0
KangaRooCommented:
Any news?
0
KangaRooCommented:
The following program lists the directory contents alphabetically sorted. If you give some information about the expected format of the filenames it can be modified for the requested order.

#include <stddef.h>
#include <sys/types.h>
#include <dirent.h>
#include <string>
#include <set>
#include <iostream>

int
main (void)
{
  DIR *dp;
  set<string> files;

  dp = opendir ("./");
  if (dp != NULL)
  {
     dirent *ep;
     while (ep = readdir (dp))
       files.insert(string(ep->d_name)) ;
       
     closedir(dp);
  }
  else
    cerr <<  "Couldn't open the directory.";

  for(set<string>::iterator i = files.begin(); i != files.end();++i)
    cout << *i << endl;
  return 0;
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PrismAuthor Commented:
Thanks for the answer :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.