Der er ikke nogen standard metode, men man kunne lave en hjemmestrikket:
#include <string>
#include <sstream>
#include <windows.h>
#include <iostream>
template <typename T>
bool FromString(T &aValue, const std::string &aStr)
{
std::stringstream ss(aStr);
return ss >> aValue;
}
int FindNumber(const std::string aDir, const std::string& aBaseName, const std::string aExt)
{
WIN32_FIND_DATA FindFileData;
std::string Pattern = aDir + aBaseName + "*" + aExt;
HANDLE FindHandle = FindFirstFile(Pattern.c_str(), &FindFileData);
if(FindHandle == INVALID_HANDLE_VALUE)
{
std::cerr << "Ups, failed, or no files" << std::endl;
return 0;
}
int Nr = 0;
do
{
if(!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) // Ignore folders
{
std::string Name = FindFileData.cFileName;
std::string::size_type End = Name.find('.');
if(End != std::string::npos)
Name = Name.substr(aBaseName.size(), End - aBaseName.size());
else
Name = Name.substr(aBaseName.size());
int A;
if(FromString(A, Name) && A > Nr)
Nr = A;
}
}
while(FindNextFile(FindHandle, &FindFileData));
FindClose(FindHandle);
return Nr;
}
int main()
{
int Nr = FindNumber("./", "Lars", ".txt");
std::cout << "Highest number: " << Nr << std::endl;
}
Men du kunne også kikke på tmpname og/eller tmpfile:
http://www.hmug.org/man/3/tmpnam.php