0

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);
}
Pukacza
  • 7
  • 1
  • 7
  • 2
    Try having `init_current` return a `char *` instead, and then copy that into `ainpath` in `current_live_read` instead of just setting character 33 of that string. (it doesn't have anything to do with freeing memory - memory of locals is freed automatically). – 500 - Internal Server Error Aug 27 '19 at 12:03
  • 2
    @Pukacza This statement ainpath[33]=init_current(); does not make sense. – Vlad from Moscow Aug 27 '19 at 12:11
  • If you want to modify a string buffer in C you have to pass the buffer as an argument. – Gem Taylor Aug 27 '19 at 12:29
  • There's just too many basic mistakes. First of all you need to read compiler warnings and fix all the bugs it points out. Then you need to study scope of variables as shown in the linked duplicate. – Lundin Aug 27 '19 at 12:37

1 Answers1

0

There are quite a few things going wrong here.

return(ainpath);

You are returning the pointer to a local array, which is going to get destroyed on function exit. Instead, allocate more memory:

buffer =  malloc(length+10); // I don't know what AIN is, but it has to fit!

Then write into buffer, not ainpath and return that, because it persists across function calls.

if (buffer)
{
    sprintf(buffer, "%s%d", buffer, AIN);
}

return buffer; // return the malloc'd buffer here

Since we are returning a pointer, init_current should be defined as (note the *):

char *init_current(void)
{
...

Finally,

ainpath[33]=init_current();

does not do what you think it does. It takes the value returned by init_current() which is a pointer, and stores it to the 34th character in ainpath, which is probably not what you want. Use a pointer instead:

int current_live_read(int *current)
{

    char* ainpath;
    ainpath=init_current();

    int curr;

    FILE *file = fopen(ainpath, "r");
    free(ainpath); // Free when you are done using the buffer

    if(!file)
        return -1;

    fscanf(file, "%4d", &curr);
    if(!feof (file))
    {

    }
    fclose(file);

    *current=curr;
    return(0);
}
th33lf
  • 2,177
  • 11
  • 15
  • It works fine, thank you so much. But there is still another problem. In the second call of: "fscanf(file, "%4d", &curr);" i have error No source available for "__isoc99_fscanf() at 0x1cf80". That's beacouse in the "FILE *file = fopen(ainpath, "r");" the pointer *file is 0 in the second call of that function. I putted the free(file) but it isn't work. – Pukacza Aug 27 '19 at 13:09
  • @Pukacza: Probably because fopen() is failing. The file may not exist or may be open from another thread. You also need to check `file` for NULL before calling fscanf. Updated the code. – th33lf Aug 27 '19 at 13:20
  • Yes that's true the file is giving NULL. So should I somehow close the file in that case or what? – Pukacza Aug 27 '19 at 13:30
  • @Pukacza If open fails, there's nothing much you can do except flag an error and exit. What you need to do is find out why it fails. Make sure no other instances of your application is running, keeping that file open. Also make sure that the file actually exists. – th33lf Aug 27 '19 at 13:41
  • @Pukacza `printf("Error opening file: %s\n", strerror(errno));` should give you an error msg that gives some clue. – th33lf Aug 27 '19 at 13:46
  • I see that every run the function fread (buffer, 1, length-1, f); adds me to the end the value of define AIN which is 5 and I don't know why. I'm cleaning the buffer every time. My bash script gives me path without 5 on the end. – Pukacza Aug 27 '19 at 14:20
  • @Pukacza try setting `buffer[length-1] = 0;` immediately after the `fread()` call. – th33lf Aug 27 '19 at 14:25
  • This solution works, thank you. But I have another problem. While it's 7th run of function init_current(); I get the error: "No source available for "_int_malloc() at 0x25f92"" for the function: " fseek (f, 0, SEEK_END);" but the *f giving me proper path, then I don't know why this have no source. The file from the path in Linux os have the 777. – Pukacza Aug 27 '19 at 15:00
  • @Pukacza "No source available" probably means that the debugger cannot find the source code for the function, which it obviously can't for `malloc` since it is in a library. The actual error is somewhere else, which you probably have missed. – th33lf Aug 27 '19 at 15:45