Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 679
  • Last Modified:

Relative paths in C++

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
Tabris42
Asked:
Tabris42
  • 7
  • 4
1 Solution
 
Jaime OlivaresSoftware ArchitectCommented:
Are you running the application from inside your compiler IDE?
Try to launch your app from command prompt or file explorer.
0
 
Tabris42Author Commented:
Good point, and I was excited to try that, but disappointed to find it still didn't work the same. :P
0
 
Jaime OlivaresSoftware ArchitectCommented:
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Jaime OlivaresSoftware ArchitectCommented:
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
 
Tabris42Author Commented:
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
 
Jaime OlivaresSoftware ArchitectCommented:
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
 
Tabris42Author Commented:
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
 
Jaime OlivaresSoftware ArchitectCommented:
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
 
Tabris42Author Commented:
Ahh, that works well. Thank you!
0
 
Jaime OlivaresSoftware ArchitectCommented:
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
 
Jaime OlivaresSoftware ArchitectCommented:
Oops! You won me!
thanks for the points.
0
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.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now