Click to See Complete Forum and Search --> : Locating a File To Read


Kohinoor24
September 24th, 2002, 03:39 AM
Hi All,

In the command line of my program,Iam giving the path of an ini file.

say:F:\Develop\InitialFile.ini

In the same Path,I have ie: in F:\Develop.I have other files

File1
File2
File3
File4
File5
Now,I want to access these file form this location to read the contents of the files.I know how to read these files.How Can I locate these files to read?
The code should not be windows dependent as I want to compile it under linux as well

Could Anyone Help?

Thanks in advance

PaulWendt
September 24th, 2002, 06:23 AM
In my opinion, you should abstract file-paths. You create a
wrapper class that would internally handle all file-path formatting
and you could use a common format in your program. As a simple
example, you could construct a FilePath object with a path taking
forward slashes; internally it'd convert all forward slashes to
back-slashes on windows systems, but leave them alone on unix
systems. After implementing this wrapping functionality, you could
do a simple search by using std::string::find_last_of and look for
the last directory delimiter [which is '\' on WIN32, but '/' in the
unix world; with your wrapper class, you'd only have to worry
about one of them].

Instead of worrying about a wrapper class, you could also use
constants like DIRECTORY_DELIMITER explicitly in your code and
build file paths in that fashion; the disadvantage of that approach
would be that accepting user-input would be harder ... and you
wouldn't be able to have const char* filenames; you'd have to
convert them all with some sort of routine. There are tradeoffs
either way, I guess.

--Paul