Solved

Relative paths in C++

Posted on 2004-08-02
11
629 Views
Last Modified: 2008-01-09
I have a program that uses relative file paths (ie. ifstream infile("clients.txt");) to assume that you can find the file in the same directory as the .EXE.

However, it does not read the file as specified in ifstream infile("clients.txt");. Putting the literal path (C:\\C++\\clients.txt) however, does work. Why is this, and how can I get it so it takes the relative path, so I can move the program around without changing the code each time?
0
Comment
Question by:Tabris42
[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
  • 7
  • 4
11 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11699333
Are you running the application from inside your compiler IDE?
Try to launch your app from command prompt or file explorer.
0
 

Author Comment

by:Tabris42
ID: 11699380
Good point, and I was excited to try that, but disappointed to find it still didn't work the same. :P
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 250 total points
ID: 11699432
Try to obtain your .exe filepath:

          char path[MAX_PATH+1] ;
          GetModuleFileName( NULL, path, MAX_PATH);

          char *slashPos = strrchr(path, '\\') ;
          if (slashPos)
               *slashPos = 0;   // trim filename, leave only path

Then you can use:
          SetCurrentDirectory(path);

0
Independent Software Vendors: 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!

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11699445
Beside your problem, when you run from Visual C++ IDE, current directory is your project directory.
Silly question: Are you sure you are generating your .exe file in C:\\C++   ? Check date and time.
0
 

Author Comment

by:Tabris42
ID: 11699482
That's what I thought. I'm actually using Dev C++, not Visual C++. I'm unsure of how to implement the code you've posted?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11699523
If you are working in Windows environment, my code should work because GetModuleFileName and SetCurrentDirectory are both WinAPI functions, not related to Visual C++.
0
 

Author Comment

by:Tabris42
ID: 11699607
Ok, the code compiles correctly.. but how would i use it to make my example above look for clients.txt in the program directory?
This seems a little needlessly complex.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11699650
This must be made only once when you start your app, then every time you try to open a file, it will be made in the current directory, even you can encapsule it in a function you can invoke from main() function at the beginning of your program.
0
 

Author Comment

by:Tabris42
ID: 11699667
Ahh, that works well. Thank you!
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11699674
Something like:

void InitDirectory()
{
          char path[MAX_PATH+1] ;
          GetModuleFileName( NULL, path, MAX_PATH);

          char *slashPos = strrchr(path, '\\') ;
          if (slashPos)
               *slashPos = 0;   // trim filename, leave only path

          SetCurrentDirectory(path);
}

Then invoke this function at the first line of your main or WinMain function.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11699677
Oops! You won me!
thanks for the points.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

696 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