get current directory in ANSI C

P

pdmountaineer

hi all,

I made an application in MATLAB, but to make code fast and efficient, I
use ANSI C routines, which I compile under MATLAB as MEX files (MATLAB
executables).

To protect my C routines, I use a function "copyright.c" that makes the
application stop if it has expired (1 month for example). Therefore I
used the ANSI C function <time_t time(time_t *tp)>.
If the application has expired, I would also like to have the files in
the current directory to be removed. Therefore I would like to use the
ANSI C function <int remove(const char *filename)>, but how can I get
the current directory?
 
B

Ben Pope

hi all,

I made an application in MATLAB, but to make code fast and efficient, I
use ANSI C routines, which I compile under MATLAB as MEX files (MATLAB
executables).

To protect my C routines, I use a function "copyright.c" that makes the
application stop if it has expired (1 month for example). Therefore I
used the ANSI C function <time_t time(time_t *tp)>.
If the application has expired, I would also like to have the files in
the current directory to be removed. Therefore I would like to use the
ANSI C function <int remove(const char *filename)>, but how can I get
the current directory?

I'm pretty sure:
int remove(const char *filename)
is not ANSI C.

Obtaining the current directory is also not possible in ANSI C, so I
suggest you use OS specific functions. As such it is off topic here.

Ben Pope
 
R

Rolf Magnus

hi all,

I made an application in MATLAB, but to make code fast and efficient, I
use ANSI C routines, which I compile under MATLAB as MEX files (MATLAB
executables).

Then you should go to an ANSI C newsgroup (comp.lang.c comes to mind).
To protect my C routines,

From what?
I use a function "copyright.c" that makes the application stop if it has
expired (1 month for example). Therefore I used the ANSI C function
<time_t time(time_t *tp)>. If the application has expired, I would also
like to have the files in the current directory to be removed.

So if the user had some files in that directory, he will lose them all
without any previous note? That could become quite expensive for you if
users lose important data.
Therefore I would like to use the ANSI C function <int remove(const char
*filename)>, but how can I get the current directory?

You can't. Neither standard C, not standard C++ offer any functionality to
handle directories.
 
D

Default User

Marco said:
you'll probably get a quicker answer to that in comp.lang.c

Not really, as the answer is "you can't".

Also, please read the information below.



Brian
 
G

Guozhong Cao

basically I have two solutions:

1. maybe it is not feasible for you, because it uses VC++, see the
following code snap:

#include <windows.h>
#include <iostream.h>

void main()
{
char path[50];

::GetCurrentDirectory(50,path);
cout<<path<<endl;
}

2. well, this is a little stupid, but it will work.

#include <stdlib.h>
void main()
{
system("dir >list.txt");
}

system("dir >list.txt");

calls a shell to execute the command dir and save the files and
directly in the file "list.txt", then you can use C to read this file,
well, I think you already know what to do next.

Good luck
 
M

Michiel.Salters

how can I get the current directory?

Simply: the current (or default) directory is by definition the one you
get if you
don't specify another. You can't specify another directory in C++, so
the current
one is the one you'll get.

HTH,
Michiel Salters
 
P

pdmountaineer

hi Michiel, thank you for the quick response; I managed to delete the
MEX files (that are responsible for the application) from the current
working directory, so now the application destroys itself when the it
has expired. Of course, the one MEX file that is responsible for the
removal of all the other MEX files cannot be deleted, as you cannot
delete a file while it is being executed. Do you think there is a
solution to that? Or should I do this in another way?

kind regards, Pi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top