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 Parsing in C++ (Simple Question)

String Parsing in C++ (Simple Question)

Scheduled Pinned Locked Moved C / C++ / MFC
c++jsontutorialquestion
10 Posts 9 Posters 0 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.
  • K Offline
    K Offline
    ksaw123
    wrote on last edited by
    #1

    Hi All I have this string /work1/data/xxxx/yyy/file_name.txt how to extract "only" the path name without the file name in C++ thanks

    V P C C S 6 Replies Last reply
    0
    • K ksaw123

      Hi All I have this string /work1/data/xxxx/yyy/file_name.txt how to extract "only" the path name without the file name in C++ thanks

      V Offline
      V Offline
      vipin_nvk
      wrote on last edited by
      #2

      Try out splitpath API, it will meet your requirement, check out MSDN for the syntax Thanx

      K 1 Reply Last reply
      0
      • V vipin_nvk

        Try out splitpath API, it will meet your requirement, check out MSDN for the syntax Thanx

        K Offline
        K Offline
        ksaw123
        wrote on last edited by
        #3

        No , I need C++ buitl-in class or a play around not Microsoft C++

        C N 2 Replies Last reply
        0
        • K ksaw123

          No , I need C++ buitl-in class or a play around not Microsoft C++

          C Offline
          C Offline
          Chandrasekharan P
          wrote on last edited by
          #4

          you need to create your own function for this. go ahead and try it. if you are stuck ask for help.

          1 Reply Last reply
          0
          • K ksaw123

            No , I need C++ buitl-in class or a play around not Microsoft C++

            N Offline
            N Offline
            N a v a n e e t h
            wrote on last edited by
            #5

            Tried boost tokenizer[^]? :)

            Navaneeth How to use google | Ask smart questions

            1 Reply Last reply
            0
            • K ksaw123

              Hi All I have this string /work1/data/xxxx/yyy/file_name.txt how to extract "only" the path name without the file name in C++ thanks

              P Offline
              P Offline
              PrafullaVedante
              wrote on last edited by
              #6

              Use the following line of code.

              dirPath =fullFilePath.substr(0,(fullFilePath.find_last_of(L"\\")));

              Prafulla Vedante

              1 Reply Last reply
              0
              • K ksaw123

                Hi All I have this string /work1/data/xxxx/yyy/file_name.txt how to extract "only" the path name without the file name in C++ thanks

                C Offline
                C Offline
                chandu004
                wrote on last edited by
                #7

                traverse the char array from end (in reverse way) till you see a / and then replace '\0' there.

                -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

                1 Reply Last reply
                0
                • K ksaw123

                  Hi All I have this string /work1/data/xxxx/yyy/file_name.txt how to extract "only" the path name without the file name in C++ thanks

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #8

                  const char * path = "/work1/data/xxxx/yyy/file_name.txt";
                  const char * filename;
                  filename = path+strlen(path);
                  while (filename != path && *(filename) != '/')
                  filename--;
                  if ( filename != path) filename++;

                  :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  1 Reply Last reply
                  0
                  • K ksaw123

                    Hi All I have this string /work1/data/xxxx/yyy/file_name.txt how to extract "only" the path name without the file name in C++ thanks

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

                    If you want simple hack, then

                    std::string s = "/work1/data/xxxx/yyy/file_name.txt";

                    std::string::size_type lastSep = s.find_last_of('/');
                    if (lastSep != std::string::npos)
                    s.erase(lastSep);

                    If you want a nice cross-platform for manipulating file paths, I'll recommend Boost.FileSystem[^], where you can do this:

                    boost::filesystem::path justThePath("/work1/data/xxxx/yyy/file_name.txt");
                    justThePath.remove_filename();

                    Oh - and it'll happily deal with Windows vs Unix path conventions.

                    Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                    1 Reply Last reply
                    0
                    • K ksaw123

                      Hi All I have this string /work1/data/xxxx/yyy/file_name.txt how to extract "only" the path name without the file name in C++ thanks

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #10

                      ksaw123 wrote:

                      how to extract "only" the path name without the file name in C++

                      Have you tried PathRemoveFileSpec()?

                      "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                      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