String manipulation.
-
Hi Guys, I want to write an function which will accept an file name(along with complete path), append an prefix to it (say Admin to the file name from c:\Sample\sam.txt to c:\Sample\Adminsam.txt). I have written an function via character array manipulation and given below.
#define ADMIN_PREFIX _T("Admin") static void RenameToAdminFile(LPCTSTR szFilePath/*in*/, LPTSTR szAdminFilePath/*out*/) { TCHAR szTempFilePath[MAX_PATH] = _T(""), *pCh; TCHAR szFileName[MAX_PATH] = _T(""); _tcscpy(szTempFilePath, szFilePath); bool bValidPath = false; if (pCh = _tcsrchr( szTempFilePath, _T('\\'))) { pCh++; // Keep trailing "\\" _tcscpy(szFileName, pCh); *pCh = _T('\0'); bValidPath = true; } else { if (pCh = _tcsrchr( szTempFilePath, _T(':'))) { pCh++; // Keep trailing ":" _tcscpy(szFileName, pCh); *pCh = _T('\0'); _tcscat(szTempFilePath, _T("\\")); bValidPath = true; } else { *szTempFilePath=_T('\0'); } } if(bValidPath) { TCHAR szTempAdminFileName[MAX_PATH] = ADMIN_PREFIX; _tcscat(szTempAdminFileName, szFileName); _tcscat(szTempFilePath, szTempAdminFileName); } _tcscpy(szAdminFilePath, szTempFilePath); return; }
Are there any STL API's (Not CString methods)that I could use to achieve the same? An code snippet will be of great help. -
Hi Guys, I want to write an function which will accept an file name(along with complete path), append an prefix to it (say Admin to the file name from c:\Sample\sam.txt to c:\Sample\Adminsam.txt). I have written an function via character array manipulation and given below.
#define ADMIN_PREFIX _T("Admin") static void RenameToAdminFile(LPCTSTR szFilePath/*in*/, LPTSTR szAdminFilePath/*out*/) { TCHAR szTempFilePath[MAX_PATH] = _T(""), *pCh; TCHAR szFileName[MAX_PATH] = _T(""); _tcscpy(szTempFilePath, szFilePath); bool bValidPath = false; if (pCh = _tcsrchr( szTempFilePath, _T('\\'))) { pCh++; // Keep trailing "\\" _tcscpy(szFileName, pCh); *pCh = _T('\0'); bValidPath = true; } else { if (pCh = _tcsrchr( szTempFilePath, _T(':'))) { pCh++; // Keep trailing ":" _tcscpy(szFileName, pCh); *pCh = _T('\0'); _tcscat(szTempFilePath, _T("\\")); bValidPath = true; } else { *szTempFilePath=_T('\0'); } } if(bValidPath) { TCHAR szTempAdminFileName[MAX_PATH] = ADMIN_PREFIX; _tcscat(szTempAdminFileName, szFileName); _tcscat(szTempFilePath, szTempAdminFileName); } _tcscpy(szAdminFilePath, szTempFilePath); return; }
Are there any STL API's (Not CString methods)that I could use to achieve the same? An code snippet will be of great help.Use
_tsplitpath
to separate the drive letter, folder name, file name and extension into separate variables. Now you can change the filename by attaching "Admin" into another buffer using_tcscpy_s
and_tcscat_s
and then stitch the full path back together using_stprintf_s
.«_Superman_» I love work. It gives me something to do between weekends.
-
Hi Guys, I want to write an function which will accept an file name(along with complete path), append an prefix to it (say Admin to the file name from c:\Sample\sam.txt to c:\Sample\Adminsam.txt). I have written an function via character array manipulation and given below.
#define ADMIN_PREFIX _T("Admin") static void RenameToAdminFile(LPCTSTR szFilePath/*in*/, LPTSTR szAdminFilePath/*out*/) { TCHAR szTempFilePath[MAX_PATH] = _T(""), *pCh; TCHAR szFileName[MAX_PATH] = _T(""); _tcscpy(szTempFilePath, szFilePath); bool bValidPath = false; if (pCh = _tcsrchr( szTempFilePath, _T('\\'))) { pCh++; // Keep trailing "\\" _tcscpy(szFileName, pCh); *pCh = _T('\0'); bValidPath = true; } else { if (pCh = _tcsrchr( szTempFilePath, _T(':'))) { pCh++; // Keep trailing ":" _tcscpy(szFileName, pCh); *pCh = _T('\0'); _tcscat(szTempFilePath, _T("\\")); bValidPath = true; } else { *szTempFilePath=_T('\0'); } } if(bValidPath) { TCHAR szTempAdminFileName[MAX_PATH] = ADMIN_PREFIX; _tcscat(szTempAdminFileName, szFileName); _tcscat(szTempFilePath, szTempAdminFileName); } _tcscpy(szAdminFilePath, szTempFilePath); return; }
Are there any STL API's (Not CString methods)that I could use to achieve the same? An code snippet will be of great help.std::string RenameToAdminFile(std::string const& file)
{
std::string::size_type lastSep = file.find_last_of('\\');
if (lastSep != std::string::npos)
{
return file.substr(0, lastSep+1) + "Admin" + file.substr(lastSep+1);
}
else
{
return "Admin" + file;
}
}? Hack-amungous, of course, as it assumes various things. As I suggested to someone else today - Boost.FileSystem[^] is a nice (and cross-platform) path manipulation library.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Use
_tsplitpath
to separate the drive letter, folder name, file name and extension into separate variables. Now you can change the filename by attaching "Admin" into another buffer using_tcscpy_s
and_tcscat_s
and then stitch the full path back together using_stprintf_s
.«_Superman_» I love work. It gives me something to do between weekends.
Thanks for the Info. Yes. You can use _wsplitpath to split the file path into Drive letters, directory, filename, and file extension and now you can do any manipulation with any of the individual members like appending the file name with an string. Now you can put individual file info togather using the api _makepath.
-
Use
_tsplitpath
to separate the drive letter, folder name, file name and extension into separate variables. Now you can change the filename by attaching "Admin" into another buffer using_tcscpy_s
and_tcscat_s
and then stitch the full path back together using_stprintf_s
.«_Superman_» I love work. It gives me something to do between weekends.
Good spot - wasn't aware of that one. There is now a 'safe' version of that[^] that MS propose you use instead...
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p