Solved

Relative paths in C++

Posted on 2004-08-02
11
613 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
  • 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now