Link to home
Start Free TrialLog in
Avatar of Prism
Prism

asked on

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.
Avatar of Gandalf32
Gandalf32

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.
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?
Avatar of Prism

ASKER

eww... well, the code I'm writing has to work in Unix and NT. So I guess i need answers for both systems.
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");
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.
Avatar of ozo
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.
"timestamp is the name" I missunderstood that.  ozo, what is available for enumerating files in a directory?
on a unix system, see
man readdir
And on Windows see FindFirstFile() and FindNextFile().
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
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?
readdir is sometimes available for Windows (at least with Borland and MingW32) so check your compiler manuals.
findfirst etc are NOT ANSI
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.
And so, once more it depends on the compiler you're using. I just love standards....
Any news?
ASKER CERTIFIED SOLUTION
Avatar of KangaRoo
KangaRoo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Prism

ASKER

Thanks for the answer :)