C++ file manipulation library?

Discussion in 'Programming' started by scootermaster, Mar 4, 2008.

  1. scootermaster

    scootermaster Platinum Member

    Joined:
    Nov 29, 2005
    Messages:
    2,411
    Likes Received:
    0
    I'm using straight Linux g++ (version 3.something, I believe) and I just need a simple file handling library. For example, if on the command-line, someone enters in ../my/dir/foo.txt I want to be able to strip the directory part and the file name part. That's actually the biggest thing, but anything else that will help with handling files would be cool.

    The reason is I have this project that takes input files, and I put them in a centralized directory for cleanlieness. But then I write about 3 files based in the input filename. So based on foo.txt, I write foo.1.csv and foo.1.dot or whatever, but if I just use the filename and append, you can see how they get written to the old directory. Again, for cleanliness, it would be nice to stick them in their own dirs.


    Help?
     
  2. Loading...

    Similar Threads - file manipulation library Forum Date
    What's the best cross platform GUI library for C++? Programming Aug 25, 2017
    C programming with directories and files Programming Jul 17, 2017
    Renaming files in aggregate using Powershell Programming Nov 27, 2016

  3. Crusty

    Crusty Lifer

    Joined:
    Sep 30, 2001
    Messages:
    12,688
    Likes Received:
    1
    Just find the location of the last / in the filepath, and do a substring from there to the end.
     
  4. Markbnj

    Markbnj Elite Member <br>Moderator Emeritus
    Moderator

    Joined:
    Sep 16, 2005
    Messages:
    15,688
    Likes Received:
    11
  5. scootermaster

    scootermaster Platinum Member

    Joined:
    Nov 29, 2005
    Messages:
    2,411
    Likes Received:
    0
    Weird. I thought I checked boost. I'm going to be using their graph library.

    But this seems like it might very well do it.

    Thanks so much!
     
  6. blackllotus

    blackllotus Golden Member

    Joined:
    May 30, 2005
    Messages:
    1,875
    Likes Received:
    0
    Look at the recls library

    EDIT: (Its not in boost)
     
  7. programmer

    programmer Senior member

    Joined:
    Mar 12, 2003
    Messages:
    412
    Likes Received:
    0
    strrchr is your friend ... maybe you could do something like this without a third-party lib


    #include <string.h>
    #include <stdio.h>

    int main(int argc, char* argv[])
    {
    char* path = 0;
    char* filename = 0;

    if (argv[1])
    {
    path = argv[1]; // just for clarity

    filename = strrchr(path, '/');
    if (filename) {

    *filename++ = 0;

    // this will null terminate path and then
    // move the filename pointer to the first
    // char of the filename... which could be
    // a null terminator!

    if (*filename == '\0')
    filename = 0;
    }

    printf("The path is %s\n", path ? path : "null");
    printf("The filename is %s\n", filename ? filename : "null");
    }

    return 0;
    }