C++ file manipulation library?

scootermaster

Platinum Member
Nov 29, 2005
2,411
0
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?
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Just find the location of the last / in the filepath, and do a substring from there to the end.
 

programmer

Senior member
Mar 12, 2003
412
0
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;
}