Re: Loading numbered files in sequence
"Giff" <Giffnews@gmail.com> ha scritto nel messaggio
news:27355b07-f879-4072-a49f-59c3c2df974f@y38g2000hsy.googlegroups.com...
Now, however, I don't seem to be able to make them read some files in
the right order. The files are named something like CCC_NNN_NNNNNN.ext
where C are letters and N numbers. The last sequence of numbers tells
me the order in which to read the files (not necessarily starting from
0) but _findnext() does not seem to care and loads the files in a
different order.
Any ideas on how to have the job done the right way?
As general guidelines, I agree with what others wrote.
You may find here a sample C++ code that I would use (compiled fine on
VS2008):
Note that AfxMessageBox is an MFC function to display text in message-boxes,
remove that and substitute with whatever you want.
I used std::vector as container of CString's (I like CString class).
I also used std::sort for sorting algorithm (defining a custom compare
function, named "FileNameLesser, which does sorting basing only on last 6
numbers of file names NNNNNN - I'm not sure if that is what you asked in
your original post... if not, you can just modify the implementation of
FileNameLesser.)
I used _tfindfirst instead of findfirst, to make code Unicode-aware (it
compiled fine in VS2008 default Unicode builds).
There are comments in the code, to explain things.
<code>
//////////////////////////////////////////////////////////////////////////
//
// Returns whether first filename string is lesser than the second,
// basing on custom sorting method.
//
bool FileNameLesser( const CString & filename1, const CString & filename2 )
{
//
// Strings are in this format:
//
// CCC_NNN_NNNNNN.ext
// 012345678 <-- start position for CString.Mid = 8
// ****** <-- count for CString.Mid = 6
//
// Sort based on last sequence of numbers: NNNNNN
//
// Extract the sorting-numbers (substring) from original strings
CString pos1 = filename1.Mid(8, 6);
CString pos2 = filename2.Mid(8, 6);
// Compare the sorting numbers of each string
return ( pos1 < pos2 );
}
//
// Filenames Sorting Test
//
void SortTest()
{
// Where to find files
CString fileSpec = _T("I:\\Test\\*.txt");
//
// Init find job
//
_tfinddata_t fileInfo;
intptr_t findHandle = _tfindfirst( fileSpec, &fileInfo );
if ( findHandle == -1 )
{
// No file or error...
// ...
// ...
AfxMessageBox( _T("No file or error...") );
return;
}
//
// Will store file names in vector<CString> container
//
typedef std::vector<CString> StringList;
StringList fileNames;
//
// Find loop
//
do
{
// Add file name to file name list
fileNames.push_back( fileInfo.name );
} while ( _tfindnext( findHandle, &fileInfo ) == 0 );
// Close find job
_findclose( findHandle );
//
// Sort the string in the array - using custom sorting
//
std::sort( fileNames.begin(), fileNames.end(), FileNameLesser );
//
// Do whatever on sorted filename strings:
//
// - just display them here
CString sortedFileNames;
for ( size_t i = 0; i < fileNames.size(); i++ )
{
sortedFileNames += fileNames.at(i);
sortedFileNames += CString(_T("\n") );
}
AfxMessageBox( sortedFileNames );
return;
}
//////////////////////////////////////////////////////////////////////////
</code>
HTH,
Giovanni