Solved

Relative paths in C++

Posted on 2004-08-02
11
624 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
Technology Partners: 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Title # Comments Views Activity
c++ syntax question 9 63
object oriented programming comparison 5 92
visual C++ 1 37
cmake and message 1 18
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

738 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