Recent

Author Topic: _IO_FILE in C has corresponding pointer in Pascal?  (Read 2359 times)

smancini

  • Newbie
  • Posts: 2
_IO_FILE in C has corresponding pointer in Pascal?
« on: February 05, 2018, 08:59:56 pm »
I am loading (in my pascal project) dynamically a library that was written in C and has some methods that receive a file pointer as parameter.... let say:

/* fopen example */
#include <stdio.h>
FILE * pFile;
pFile = fopen ("myfile.txt","r");
DoSomething(pFile);

in my pascal project i have an implementation for the DoSomething method that receive as parameter a Pointer.

In the C code the pFile is (typedef struct _IO_FILE)

I need to know the readfile pascal code that give me a pointer as result to pass to my method.

for instance, if i try something like:
Instream: TFilestream; 
Instream := TFilestream.Create(fpath, fmOpenRead);
DoSomething(Instream);

this code compile but does not work

An unhandled exception occurred at $00007F27FFDAC888:
EAccessViolation: Access violation

Can any of you help me with this?

thanks!! Sebastian

 

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12712
  • FPC developer.
Re: _IO_FILE in C has corresponding pointer in Pascal?
« Reply #1 on: February 05, 2018, 09:16:11 pm »
Free Pascal has no C cooked file I/O. FILE* is internal libc file type.

The choice is between native pascal I/O (assign/rewrite/write/read/close) and OS level handle based I/O (open/read/write/close that take ints or thandles).

For 32-bit Linux there might be some old routines in unit libc, but they are only half supported, and personally I have bad experiences with them.
« Last Edit: February 05, 2018, 09:51:38 pm by marcov »

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: _IO_FILE in C has corresponding pointer in Pascal?
« Reply #2 on: February 05, 2018, 09:47:46 pm »
It seems to me that _IO_FILE structure is implementation dependent. I googled fopen source code and the few results showed different details.

If you really have a good reason, you can find the implementation of the library you are using and replicate it in Pascal.

ahydra

  • New Member
  • *
  • Posts: 19
Re: _IO_FILE in C has corresponding pointer in Pascal?
« Reply #3 on: February 05, 2018, 10:38:23 pm »
There's "Text" and a bunch of related types and functions, which do similar things to FILE* and f* (fopen, fclose etc) in C. See http://wiki.freepascal.org/File_Handling_In_Pascal

As for your TFileStream example, is the crash in the code you've shown or somewhere further down, for example in DoSomething()? AFAIK TFileStream's constructor will throw an exception if it couldn't open the file so you shouldn't have any issues with Instream being nil, but it's probably worth checking it anyway.

ahydra

 

TinyPortal © 2005-2018