?
Solved

Relative paths in C++

Posted on 2004-08-02
11
Medium Priority
?
635 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 1000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

770 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