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?
 
KangaRooConnect With a Mentor Commented:
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
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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
 
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
 
PrismAuthor Commented:
Thanks for the answer :)
0
All Courses

From novice to tech pro — start learning today.