I have problem with multiple calling the function that is returning the char value. I want to reassign the returning value from that function to the char variable in another function. Here is the code of function where I'm calling the function init_current():
int current_live_read(int *current)
{
char ainpath[33];
ainpath[33]=init_current();
char *filename = ainpath;
int curr;
FILE *file = fopen(filename, "r");
fscanf(file, "%4d", &curr);
if(!feof (file))
{
}
fclose(file);
*current=curr;
return(0);
}
In this function I'm calling the function init_current(). When I'm calling it for the first time I have proper return value of ainpath[33] variable. But when I'm calling the current_live_read(int *current) for the second time I have error in fscanf couse the variable ainpath[33] after second call is "Name : ainpath Details:'\0' , "\027Î\001\0Túÿ¾\0\037ã\225r.16\0\0\b\0" Default:0xbefffa28 Decimal:-1090520536" which is not correct for sure. I think that i need to free somehow the array ainpath[33] but I don't know how.
Here is the code of init_current():
char init_current(void)
{
system("sudo echo cape-bone-iio > /sys/devices/bone_capemgr.*/slots"); //Init ADC
system(AINpath);
//int file;
char ainpath[33];
char *filename = "/root/LED_Tester_V1/CurrentRead/pathbuf";
char * buffer = 0;
long length;
FILE * f = fopen (filename, "rb");
if (f)
{
fseek (f, 0, SEEK_END);
length = ftell (f);
fseek (f, 0, SEEK_SET);
buffer = malloc (length);
if (buffer)
{
fread (buffer, 1, length-1, f);
}
fclose (f);
}
if (buffer)
{
sprintf(ainpath, "%s%d", buffer, AIN);
}
return(ainpath);
}