What Monkey type corresponds to FILE *?
Monkey Forums/Monkey Programming/What Monkey type corresponds to FILE *?
| ||
I see that Monkey uses FILE * in various files, but how do you get a FILE * variable back to Monkey? I tried with Field f:Object, and it works, it creates a file, writes a correct Int value into it, and closes it, but Monkey still crashes with a NULL pointer exception when returning the FILE * to Monkey as Object. Since it works, maybe I should rather remove the NULL pointer crashing from Monkey? Maybe it's using somekind of GC which causes it to crash, although there's no need for GC here. My fileio.cpp looks like this: // fileio.cpp Object* openfile(String s, String mode) { FILE *f=NULL; f=fopen(s.ToCString<char>(),mode.ToCString<char>()); return (Object *)f; } void closefile(Object *f) { if((FILE *)f)fclose((FILE *)f); } void writefileint(Object *f,int i) { fwrite(&i,sizeof(i),1,(FILE *)f); } int readfileint(Object *f) { int i=0; fread(&i,sizeof(i),1,(FILE *)f); return i; } And the Monkey file looks like this: Import mojo #If TARGET="html5" Then Import "fileio.js" ' not yet implemented! #Else Import "fileio.cpp" ' tell the compiler to include code from fileio.cpp #Endif Extern ' the C++ function mytestfunc1 is renamed as MyFunc1 in Monkey Function OpenFile:Object(s:String,mode:String)="openfile" Function CloseFile:Void(f:Object)="closefile" Function WriteFileInt:Void(f:Object,i:Int)="writefileint" Function ReadFileInt:Int(f:Object)="readfileint" Public Class Game Extends App Field i:Int Field f:Object Method OnCreate() i=123 f=OpenFile("testfile.bin","wb") ' call the c++ function here WriteFileInt(f,i) CloseFile(f) SetUpdateRate 60 ' this sets the update rate in FPS End Method OnRender() Cls 30,30,30 ' Cls is needed, else nothing will be drawn DrawText "Hello World",0,0 DrawText "i="+i,0,20 End End Function Main() New Game End |
| ||
You could try using ints in monkey and doing the casting in C++. Sine you know, pointers are ints. |
| ||
Sine you know, pointers are ints Only on 32-bit builds? |
| ||
@JIM: Wow you're awesome, thanks a lot! Now it works and doesn't crash anymore: // fileio.cpp int openfile(String s, String mode) { FILE *f=NULL; f=fopen(s.ToCString<char>(),mode.ToCString<char>()); return (int)f; } void closefile(int f) { if((FILE *)f)fclose((FILE *)f); } void writefileint(int f,int i) { fwrite(&i,sizeof(i),1,(FILE *)f); } int readfileint(int f) { int i=0; fread(&i,sizeof(i),1,(FILE *)f); return i; } Import mojo #If TARGET="html5" Then Import "fileio.js" ' not yet implemented! #Else Import "fileio.cpp" ' tell the compiler to include code from fileio.cpp #Endif Extern Function OpenFile:Int(s:String,mode:String)="openfile" Function CloseFile:Void(f:Int)="closefile" Function WriteFileInt:Void(f:Int,i:Int)="writefileint" Function ReadFileInt:Int(f:Int)="readfileint" Public Class Game Extends App Field i:Int Field f:Int Method OnCreate() i=123 f=OpenFile("testfile.bin","wb") ' call the c++ function here WriteFileInt(f,i) CloseFile(f) SetUpdateRate 60 ' this sets the update rate in FPS End Method OnRender() Cls 30,30,30 ' Cls is needed, else nothing will be drawn DrawText "Hello World",0,0 DrawText "i="+i,0,20 End End Function Main() New Game End |
| ||
I guess 32-bits is the future after all ! :-) |
| ||
Well yeah, forgot to mention that :) But as long as it works, 32-bit is the future :D |
| ||
I think 64-bit will never work completely, as long 32-bit supporting hardware still exists. Developers are lazy and don't follow the newest technology if the old technology still exists and works. Maybe when all hardware is 128-bit, then developers start slowly to develop 64-bit programs :) Well, looking at the mirror, I still use 32-bit Windows XP, and have no plans to upgrade to 32-bit or 64-bit Windows 7 until 2020 or later. I tried it a few times, and it was slower than XP with OpenGL apps. |
| ||
Ah... I forget all those people who use Windows... never mind! :-) |
| ||
Yeah, I'm trying to, and I have already 5 Linuxes (Debian, openSUSE, Ubuntu, Mint, Debian64), 1 Android and 1 BSD in my GRUB loader. But Monkey was again released first on Windows and Mac, so where's my Linux and BSD version of Monkey? :) |
| ||
so where's my Linux and BSD version of Monkey? I guess you'll have to wait for Mark to sort that out. I could, but there's not much point if Mark is going to do it anyway. |