Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. String manipulation.

String manipulation.

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structuresjsonhelpquestion
5 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Comp_Users
    wrote on last edited by
    #1

    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.

    _ S 2 Replies Last reply
    0
    • C Comp_Users

      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.

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      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.

      S C 2 Replies Last reply
      0
      • C Comp_Users

        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.

        S Offline
        S Offline
        Stuart Dootson
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • _ _Superman_

          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.

          C Offline
          C Offline
          Comp_Users
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • _ _Superman_

            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.

            S Offline
            S Offline
            Stuart Dootson
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups