Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ChDir in Visual C++

Status
Not open for further replies.

Frink

Programmer
Mar 16, 2001
798
GB
Hallo,

Is there an equivalent in C++ to ChDir in Visual Basic?
I want to change the current directory to the directory containing the executable, at the start of my program, so that I can use fopen to open files within that directory elswehere in my code, without having to specify a path.

- Frink

PS. I'm not great at C, so if someone wants to give me the code to set the directory based on the command in argv[0], that would be cool
 
Cheers, I'm using _chdir as follows:
Code:
  //Set Current directory to Executable location
  char* pdest = strrchr( argv[0], '\\' );
  strncpy (pdest + 1, "\0", 1);
  _chdir (argv[0]);
[\code]

A bit norty, modifying argv[0], but it seems to work,

- Frink
 
Just a note to Frink:

why do you use strncpy? why not just do *(pdest + 1) = 0; ?

------------------
When you do it, do it right.
 
Hallo dEVooXiAm,

why do you use strncpy? why not just do *(pdest + 1) = 0; ?

Because *(pdest + 1) = 0; makes absolutely no sense to me!
I'm a very reluctant novice C programmer.

*(pdest + 1) = 0; is what's wrong with C, IMHO.
Programming statements should be easily understood, as they are just the means to an end, not the end in itself. I mean that more effort should be in the design of the software, the user interface, looking at how it is to be used, and by whom, rather than getting the code syntax right.
Presumably (*pdest + 1) = 0; would mean something completely different. That's the sort of easy-to-get-wrong syntax which has no place in a modern computer language. The code should be obvious what it is trying to achieve, the language clear and unambiguous, mistakes easily spotted.

C was good for simple programs on computers with tiny memories, but things have moved on. Programs are more complex, computers more powerful. In a program of 10 lines, each line gets a lot of attention. In a program of 100,000 lines, each line is of lesser importance compared to the whole.

To me a=a+1 is 'english', understandable by anyone who has done algrebra or beginners computer programming.
a++ is not.
It might mean that the computer can map it to the assembly language better, but I want the compiler to do that for me.
It also means that the number of statements can be reduced, 1 line instead of 3 perhaps, for some coding examples. IMHO, this is not a good thing as it introduces the potential for more subtle errors and requires more knowledge on the order the line is to be processed in.

I don't mean anything personal by this rant. I will never be a C programmer, and am impressed by anyone who is good at it, but maybe it's time for a new generation of languages/compilers which allow easy to understand code to be written, but run as fast as C.

- Frink
 
Is
Code:
strncpy (pdest + 1, "\0", 1);
more clear/sensible than ordinary/obvious
Code:
pdest[1] = 0;
?
 
Oh, there might be a long and large discussion on this :)

But I would like to point one thing though. These modern programming languages (as you call them), are more useful to do things fast and easy, without much consideration on how things really work. For a programmer like me, languages like Delphi & Visual Basic dont look serious, because while programming you dont have control over various small things, that so often need to be tweaked in one way or another. And it's not super-readability that mostly is necessary in projects - that's speed.
As a simple example - try comparing 1,000,000 iterations of pdest[1] = 0 (which is a not so obvious IMO as *(pdest + 1) = 0 just because it's not an array) and strncpy(pdest + 1, 0, 1). Although the difference will be just a few secs, on a larger scale it might even come to minutes.
IMO a good programming practise is when readability and effectiveness does not spread for the cost of the other.

------------------
When you do it, do it right.
 
Hallo again,

Good points, dEVooXiAm, although I would not say that speed is the be-all and end-all. For some things, such as device drivers, speed is the most important objective. For others, however, where there is more user interaction, I would say a clear, simple HCI is more important.

I suppose I am coming from a large software company background, where large products are designed to be worked on by teams or people, and they will be upgraded throughout their life, which can be up to 50 years. In the past, when it was all written in low-level languages, mountains of documentation had to be produced to explain to whoever worked on it next, the overall design, and ethos.
With a higher level language, this is more obvious, particularly if OO is used well.

I guess there will always be a place for both, with C used where speed is more important, and other languages according to their strengths.

- Frink

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top