c language in vensim

Use this forum to post Vensim related questions.
Anna
Member
Posts: 22
Joined: Tue Jan 17, 2006 12:08 pm

Post by Anna »

Whether you solve the problem or not,give me any reply ,please.I am worried about it very much.I tried to modify program body to make VC fine but fail.
Could anyone help me?
My things are too big to attach on the web.
I can email my things for you.

[Edited on 4-18-2006 by Anna]

[Edited on 4-18-2006 by Anna]
LAUJJL
Senior Member
Posts: 1427
Joined: Fri May 23, 2003 10:09 am
Vensim version: DSS

C program

Post by LAUJJL »

Hi

I cannot help you with you C programs that I do not use with Vensim, but I can give you an advice.
Simplify you programs so that you can send them on the web.
Simplifying your programs may help you solve your problems by yourself, and will make it easier for someone else to help you.
You can simplfiy step by steps, until the program is so simple that it works.
Regards.
JJ
Administrator
Super Administrator
Posts: 4590
Joined: Wed Mar 05, 2003 3:10 am

Post by Administrator »

There is a problem with your code somewhere. When running, it crashes.

You need to run it through the Visual C++ debugger and work out where your errors are.

In this case you have a null file pointer.

double VC(int m)
{
FILE *out;
int i ,num=15; //output_num
double p[15]={0} ;

out=fopen("output.dat","r");

for (i=0;i<num;i++) {
fscanf(out, "%lf\n", &p);
return (*(p+m));
}
}


The file pointer 'out' is null which is causing the crash when you try and read from it.

Tony.
Anna
Member
Posts: 22
Joined: Tue Jan 17, 2006 12:08 pm

Post by Anna »

Thank you for LAUJJL and Tony.
I don't know why the file pointer "out " is wrong?I chang the file pointer name(out->we) to see if it can be fine but it is not(teh error is the same).This is my test that I put VC on vc++ to run and it is fine.
It can get each value in the array. So can you say clearly how to modify my VC_FUNCTION in venext.c ?
=====================================
#include<stdio.h>
#include<stdlib.h>
double VC(int);
void main()
{
int se=11;
VC(se) ;

}
double VC (int m)
{
FILE *out;
int i ,num=15;
double p[15]={0};
out=fopen("output.dat","r");
for (i=0;i<num;i++) {
fscanf(out, "%lf\n", &p);
}
//for (i=0;i<num;i++) {
// printf("%lf\n",p );
//}
//printf("%lf\n",*(p+m) );
return (*(p+m));
}
========================================

Yesterday,I said I found if vensim model give 0~4 into VC_FUNCTION,the function can work normally,otherwise error happened.But I hope it can sow 15 values which can be represented 15 variables at the same time.
I attach some file.
a dll file,venext.c,vensim model which use VC_function
output.dat.
Can anyone take a look in your free time?
Thank you.



ps. Can vensim company have a branch company in Taiwan
and know about this part?

[Edited on 4-19-2006 by Anna]

[Edited on 4-19-2006 by Anna]
Attachments
vc.rar
(78.1 KiB) Downloaded 268 times
Administrator
Super Administrator
Posts: 4590
Joined: Wed Mar 05, 2003 3:10 am

Post by Administrator »

>> I don't know why the file pointer "out " is wrong?I chang
>> he file pointer name(out->we) to see if it can be fine but it
>> is not(teh error is the same).This is my test that I put VC
>> on vc++ to run and it is fine.
Changing the name of the pointer will make no difference to the error. I suspect you are having problems here because you have not closed the file after each call.

>> It can get each value in the array. So can you say clearly
>> how to modify my VC_FUNCTION in venext.c ?
>> Yesterday,I said I found if vensim model give 0~4 into
>> VC_FUNCTION,the function can work normally,otherwise
>> error happened.But I hope it can sow 15 values which can
>> be represented 15 variables at the same time.
>> I attach some file.
>> a dll file,venext.c,vensim model which use VC_function
>> output.dat.
>> Can anyone take a look in your free time?
>> Thank you.
I can't say how to modify the function, you need to talk to the people who wrote it. Then give them access to the DLL code that ships with Vensim.

>> ps. Can vensim company have a branch company in Taiwan
>> and know about this part?
This is nothing at all to do with Vensim.

The problem you have here is that the code does not appear to work properly in the first place. Or you are just not calling it correctly. You need to talk to the person who wrote it and ask them for help, they will understand what to do.

Tony.
Anna
Member
Posts: 22
Joined: Tue Jan 17, 2006 12:08 pm

Post by Anna »

The VC_function is created by myself.I hope the 15 outputs (produced by NETWORK_FUNCTION) can be showed on the vensim so I created the function. But it have problem as I talk on the web. Is there an another writing way to do the same thing?
Administrator
Super Administrator
Posts: 4590
Joined: Wed Mar 05, 2003 3:10 am

Post by Administrator »

Try closing the file before returning at the end of the call to VC.

If that does not work, there must be an error somewhere else in your code. Vensim is calling the function fine, but when it runs for a certain amount of time, it crashes in your code because of a invalid file pointer.

double VC(int m)
{
FILE *out;
int i ,num=15; //output_num
double p[15]={0} ;
out=fopen("output.dat","r");
for (i=0;i<num;i++)
{
fscanf(out, "%lf\n", &p);
}
fclose(out);
return (*(p+m));
}
Anna
Member
Posts: 22
Joined: Tue Jan 17, 2006 12:08 pm

Post by Anna »

Now ,it can work.
The bug is that I colse file after returning p.
When I chang the position ('fclose (out) ') and put it before returning p . It work normally.
Thank you for everything you do for me.:D
I bother with you for a period time.
Thank you Tony and Bob.
Thank you very much!
Post Reply