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. C++ program to print the path without the file name!

C++ program to print the path without the file name!

Scheduled Pinned Locked Moved C / C++ / MFC
c++
20 Posts 6 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 ksaw123

    here it is the C code , it prints the base filename I want to modify it to print the following /work1/data/xxxx/yyy/ 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++; cout << filename;

    J Offline
    J Offline
    Jijo Raj
    wrote on last edited by
    #3

    You can use PathRemoveFileSpec()[^] to strip out the filename. Regards, Jijo.

    _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

    K 1 Reply Last reply
    0
    • K ksaw123

      here it is the C code , it prints the base filename I want to modify it to print the following /work1/data/xxxx/yyy/ 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++; cout << filename;

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

      And what is the question? :)

      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]

      K 1 Reply Last reply
      0
      • C chandu004

        traverse the char array from end (in reverse way) till you see a / and then replace '\0' there. did this not work for you? i found, you asked the same question on 8th july see the answers here http://www.codeproject.com/Messages/3110995/String-Parsing-in-Cplusplus-Simple-Question.aspx[^]

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

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

        it didn't work I need to print the path without the baseile name

        C 1 Reply Last reply
        0
        • J Jijo Raj

          You can use PathRemoveFileSpec()[^] to strip out the filename. Regards, Jijo.

          _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

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

          it is C/C++ program not vasuil C++

          D 1 Reply Last reply
          0
          • C CPallini

            And what is the question? :)

            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]

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

            How can I print the path without the base filename

            C 1 Reply Last reply
            0
            • K ksaw123

              it is C/C++ program not vasuil C++

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

              Use PathRemoveFileSpec() or _splitpath().

              "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

              K 1 Reply Last reply
              0
              • D David Crow

                Use PathRemoveFileSpec() or _splitpath().

                "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

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

                Which .h file those functions is in?

                D 1 Reply Last reply
                0
                • K ksaw123

                  Which .h file those functions is in?

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

                  See here.

                  "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

                  K 1 Reply Last reply
                  0
                  • D David Crow

                    See here.

                    "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

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

                    my boss I don't want to use Microsoft C++ , I am using the lagacy C/C++ actually , my code is working but i wanna print the path without the file name as your suggested function. Thanks

                    D 1 Reply Last reply
                    0
                    • K ksaw123

                      How can I print the path without the base filename

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

                      The code you posted does it. :)

                      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]

                      K 1 Reply Last reply
                      0
                      • C CPallini

                        The code you posted does it. :)

                        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]

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

                        the code i posted prints the base file name not the path I want to print the path without file name

                        C C 2 Replies Last reply
                        0
                        • K ksaw123

                          my boss I don't want to use Microsoft C++ , I am using the lagacy C/C++ actually , my code is working but i wanna print the path without the file name as your suggested function. Thanks

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

                          You might want to consider strrchr() to find the last backslash. Put a '\0' character there.

                          "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
                          • K ksaw123

                            it didn't work I need to print the path without the baseile name

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

                            this logic worked for me perfectly.

                            const char \* path = "/work1/data/xxxx/yyy/file\_name.txt";
                            char path1\[256\];
                            strcpy(path1,path);//copying the char array
                            cout << path <<"\\n";//this prints the whole filename with path.
                            int l=strlen(path);//finding the length of the path
                            for(int i=l;i>0;i--)//reverse traversing the array
                            {
                            	if(path1\[i\]=='/')//if '/' is found, then replacing it with '\\0'.
                            	{
                            		path1\[i\]='\\0';
                            		break;
                            	}
                            }
                            cout << path1<<"\\n";//this prints only the path.
                            

                            the output is as you desired.

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

                            K L 2 Replies Last reply
                            0
                            • K ksaw123

                              the code i posted prints the base file name not the path I want to print the path without file name

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

                              see here http://www.codeproject.com/Messages/3118524/Re-Cplusplus-program-to-print-the-path-without-the-file-name.aspx[^] and also try with the David crow's recent post that would be easier than mine.

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

                              1 Reply Last reply
                              0
                              • C chandu004

                                this logic worked for me perfectly.

                                const char \* path = "/work1/data/xxxx/yyy/file\_name.txt";
                                char path1\[256\];
                                strcpy(path1,path);//copying the char array
                                cout << path <<"\\n";//this prints the whole filename with path.
                                int l=strlen(path);//finding the length of the path
                                for(int i=l;i>0;i--)//reverse traversing the array
                                {
                                	if(path1\[i\]=='/')//if '/' is found, then replacing it with '\\0'.
                                	{
                                		path1\[i\]='\\0';
                                		break;
                                	}
                                }
                                cout << path1<<"\\n";//this prints only the path.
                                

                                the output is as you desired.

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

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

                                sorry, it didn't work why? look at the way you are using strcpy() function

                                C 1 Reply Last reply
                                0
                                • K ksaw123

                                  sorry, it didn't work why? look at the way you are using strcpy() function

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

                                  ksaw123 wrote:

                                  look at the way you are using strcpy() function

                                  whats the problem?

                                  -------------------------------------------- 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

                                    the code i posted prints the base file name not the path I want to print the path without file name

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

                                    you're right:

                                    const char * path ="/work1/data/xxxx/yyy/file_name.txt";
                                    char * basename;
                                    char * filename;

                                    basename = _strdup(path);
                                    if ( !basename){/*handle error*/}

                                    filename = basename+strlen(basename);

                                    while (filename != basename && *(filename) != '/')
                                    filename--;

                                    if ( filename != basename)
                                    {
                                    *(filename+1)='\0';
                                    }
                                    else
                                    *basename='\0';

                                    printf("%s\n", basename);
                                    free(basename); /* REMEMBER TO FREE */

                                    :)

                                    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
                                    • C chandu004

                                      this logic worked for me perfectly.

                                      const char \* path = "/work1/data/xxxx/yyy/file\_name.txt";
                                      char path1\[256\];
                                      strcpy(path1,path);//copying the char array
                                      cout << path <<"\\n";//this prints the whole filename with path.
                                      int l=strlen(path);//finding the length of the path
                                      for(int i=l;i>0;i--)//reverse traversing the array
                                      {
                                      	if(path1\[i\]=='/')//if '/' is found, then replacing it with '\\0'.
                                      	{
                                      		path1\[i\]='\\0';
                                      		break;
                                      	}
                                      }
                                      cout << path1<<"\\n";//this prints only the path.
                                      

                                      the output is as you desired.

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

                                      L Offline
                                      L Offline
                                      Lost User
                                      wrote on last edited by
                                      #20

                                      You don't need to parse. Use SHString apis.

                                      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