How to get the path of a file in VC++

Hi,


I want to find the path of a file for example test.xml.
If test.xml is located in C:\Temp\Data\test.xml, then by giving the file name (test.xml) as input, I want the path as output i.e C:\Temp\Data
I will have only one single copy of the file.
Can anyone please help me to do this?


Thanks
Madhavi
osi-sysAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Amritpal SinghCommented:
0
Amritpal SinghCommented:
easy way out ,use
GetFullPathName(string lpFileName , long nBufferLength , ref string lpBuffer , string lpFilePart )

String ls_buffer = space(255)
GetFullPathName('trala.txt' , 255, ls_buffer, '' )  
MessageBox("", "full path : "+ trim( ls_buffer))
0
AndyAinscowFreelance programmer / ConsultantCommented:
Store the path of the file (eg in registry).

When you require this path get it from the registry.  If it doesn't exist then you can find the location of the file with CFileFind.  You will need to have it in a recursive call until it finds the location of the file and stores in registry.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

osi-sysAuthor Commented:
Hi amrit_82

My XXX.xml file is in C:\Project\LogConfig\exe\XML\
and my .dsw project file is in  C:\Project\LogConfig\LogApp
and my .exe is in C:\Project\LogConfig\exe

So if I call String ls_buffer = space(255)
GetFullPathName('XXX.xml , 255, ls_buffer, '' )  

ls_buffer is returning C:\Project\XXX.xml instead of  C:\Project\LogConfig\exe\XML\XXX.xml

Can you please let me know how to get the correct path

Thanks
Madhavi
0
AndyAinscowFreelance programmer / ConsultantCommented:
If you look at GetFullPathName in help you will find it uses the current path and drive.
You need to adopt an approach as I outlined.  (If, however, this file will ALWAYS be in the same directory as your app then you can use a shortcut and get the path of the exe)
0
osi-sysAuthor Commented:
If, however, this file will ALWAYS be in the same directory as your app then you can use a shortcut and get the path of the exe ---- My file will always be in the same directory. Can you please let me know how to get the path of the exe.
 I cannot hard code and store the path in registry.

Thanks
Madhavi
0
Amritpal SinghCommented:
hi madhavi,
while going through msdn articles ,what i found is that GetFullPathName just append s the filename to the current directory path.so,i think that won't work

however if ur XML folder is always in your exe's path u can do this

1.find the path to the exe by writing this code (this would work for sure) :))

HINSTANCE hInst = AfxGetInstanceHandle();
char szFileName[MAX_PATH];
::GetModuleFileName(hInst, szFileName, MAX_PATH);

2. find the last position of "\" in szFileName (u can use FindReverse )
 
3. retrieve the string upto that position.

4.add "\XML\filename" to the resulting string

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
osi-sysAuthor Commented:
Hi amrit_82 & AndyAinscow

Thanks for all your inputs and help. I am able to retrieve the path now.

thanks
madhavi
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.