C file functions in QB64 ??? - Printable Version +- QB64 Phoenix Edition (https://staging.qb64phoenix.com) +-- Forum: QB64 Rising (https://staging.qb64phoenix.com/forumdisplay.php?fid=1) +--- Forum: Code and Stuff (https://staging.qb64phoenix.com/forumdisplay.php?fid=3) +---- Forum: Help Me! (https://staging.qb64phoenix.com/forumdisplay.php?fid=10) +---- Thread: C file functions in QB64 ??? (/showthread.php?tid=1644) Pages:
1
2
|
C file functions in QB64 ??? - Jack - 04-26-2023 I wonder if it's possible to get the C file functions working in QB64, specifically fopen, fprintf and fclose I tried the following but it won't compile Code: (Select All) Type iobuf the compiler log follows Quote:internal\c\c_compiler\bin\c++.exe -O2 -w -std=gnu++11 -DGLEW_STATIC -DFREEGLUT_STATIC -Iinternal\c\libqb/include -Iinternal\c/parts/core/src/ -Iinternal\c/parts/core/glew/include/ -DDEPENDENCY_NO_SOCKETS -DDEPENDENCY_NO_PRINTER -DDEPENDENCY_NO_ICON -DDEPENDENCY_NO_SCREENIMAGE internal\c/qbx.cpp -c -o internal\c/qbx.o I know that QB64 has file functions but I have a reason to want the C file functions <edit> fopen works, it's the other two functions that fail RE: C file functions in QB64 ??? - Jack - 04-26-2023 using the msvcrt.dll helps to avoid type clashes, and although it compiles, it crashes Code: (Select All) Type file RE: C file functions in QB64 ??? - DSMan195276 - 04-26-2023 There's a couple issues. The big one is the lack of Byvalon your file_ptrparameters to fprintf()and fclose(). Pass-by-reference is the default so QB64 will actually pass the address of the _Offsetrather than the _Offsetvalue itself, leading to the explosions you're encountering. If you instead use byval file_ptr As _Offsetthen it might work. The other issues is the types in the C++ function definitions. fprintf()and fclose()take a FILE *as their first argument, but an _Offsetis an intptr_tso you cannot pass that as a FILE *without a cast (and there's no way to insert the cast as QB64 won't do it for you). The solution here is to use either Declare Dynamic Libraryor (preferably) Declare CustomType Library, both of those cause QB64 to generate its own C++ definitions for the functions listed in the Declare Librarysection based on the types you define, so the types you provide will always match. The big catch with this is that it also means there will be no type-checking by C++ to ensure you're actually passing the right thing, so it's best to use regular Declare Libraryfor any functions where it's possible for you to provide the correct types directly (like fopen()). RE: C file functions in QB64 ??? - Petr - 04-26-2023 maybe (but just maybe) it would work using the H file, because FILE * could perhaps be defined somehow in the H file. The same problem was with glQuadric in OpenGL, which was then solved by Ashish using the H file, which I subsequently only expanded . See how he called *quad here. I assume that FILE * should also be called in a very similar way. Code: (Select All) /* And I confirm that the absence of BYVAL reliably crashes every program, I had a problem with that too, adding BYVAL fixed everything. RE: C file functions in QB64 ??? - Jack - 04-26-2023 Hi DSMan195276 you were right, byval fixed it I should have seen it, it was there right in front of me when you use a DLL you are not bothered by type correctness, just as long as the data will fit in the type it works that's why in my second attempt I used Declare Dynamic Library "msvcrt" thanks for opening my eyes Code: (Select All) Declare Dynamic Library "msvcrt" it displays hello world RE: C file functions in QB64 ??? - Kernelpanic - 04-28-2023 Quote:it displays hello worldI don't see any of that on me. However, the created text file with the correct content is available. Unfortunately, the attempt to display the text of the file on the screen did not work either: fprintf(stdout, ". . .", x, y); RE: C file functions in QB64 ??? - Jack - 04-28-2023 hello Kernelpanic use the code right above your post, it should work as is RE: C file functions in QB64 ??? - Jack - 04-28-2023 @Kernelpanic I think I know why you are having a problem, you must be using the 32-bit version of QB64 if that's the case then try the code below for 32-bit QB64 Code: (Select All) Declare Dynamic Library "msvcrt" RE: C file functions in QB64 ??? - Ultraman - 04-28-2023 I'd recommend putting the 64 bit and 32 bit code into their own $IF blocks that way no matter what architecture someone is on, the file will dynamically be ready to compile for their system, whether 32 or 64. So something like $IF 32BIT THEN 'insert 32 bit specific code $ELSE 'insert 64 bit specific code $END IF RE: C file functions in QB64 ??? - Kernelpanic - 04-28-2023 Quote:I think I know why you are having a problem, you must be using the 32-bit version of QB64The second version works, also with the 64 bit version. What have you changed? At first glance I don't see it, maybe at second glance. |