Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-10-27
8
Medium Priority
?
993 Views
Last Modified: 2013-11-20
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
0
Comment
Question by:osi-sys
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 6

Expert Comment

by:Amritpal Singh
ID: 12420848
0
 
LVL 6

Expert Comment

by:Amritpal Singh
ID: 12421132
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
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 12421523
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:osi-sys
ID: 12430534
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
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 12430696
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
 

Author Comment

by:osi-sys
ID: 12430720
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
 
LVL 6

Accepted Solution

by:
Amritpal Singh earned 80 total points
ID: 12430902
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
 

Author Comment

by:osi-sys
ID: 12430986
Hi amrit_82 & AndyAinscow

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

thanks
madhavi
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question