2021年1月20日星期三

pass FILE pointer to C function from golang

My C function takes address of FILE pointer (pointer to pointer that is, FILE **fp). I am calling this method from golang. Can someone give me a example code line for calling convention in golang.

C library code :

int fileOpen(char *filename, char* mode, FILE **fp){  *fp  = fopen (filename, mode);   if (*fp ==NULL)     return -1;   return 0;  }  int fileread(FILE *fp,char *buff ){          if((fp!=NULL)&&( fgets ( buff, 50, fp) != NULL ) )          {                  return 0;          }    return -1;  }  int fileclose(FILE *fp){    fclose(fp);    return 0;  }    
import "C"  type (          fileptr C.FILE  )    func opencfile(name string, mode string) int {          var fp *fileptr          return (int)(C.fileOpen(C.CString(name), C.CString(mode), C.FILE(&fp)));  }  

I am getting error

cannot convert _cgoBase2 (type **fileptr) to type _Ctype_struct__IO_FILE

Question:

  1. Is this the right way to call C method?
  2. how do I pass address of file pointer from golang? or how to resolve the the above error.

The called "C" method prototype is :

int fileOpen(char *filename, char* mode, FILE **fp);  
https://stackoverflow.com/questions/65819321/pass-file-pointer-to-c-function-from-golang January 21, 2021 at 07:49AM

没有评论:

发表评论