0

I'm using the write method to directly write pixels which is an array of array[(R, G, B)] of byte;. The pixels is properly allocated like so: setlength(pixels, 750000); what I do is as follows:

f := TFileStream.create(FileName, fmCreate);
written := f.write(pixels, 750000);

The problem is that the write method returns 0, videlicet it didn't write a byte from pixels. With some tests, I've discovered that it copies only up to about 20000 bytes, certainly not more than 30000 and the moment I give it more to write, it doesn't.. and returns 0.


I am new to Pascal, but I cannot find a solution to this unpleasant problem. So what am I doing wrong ?

Imobilis
  • 1,475
  • 8
  • 29
  • I've never used the fpc compiler but I do wonder what the signature on tFileStream.Write is--could it be expecting a 16 bit integer?? – Loren Pechtel Jan 24 '16 at 02:45
  • @LorenPechtel I don't know. FPC states: "This class is an encapsulation of the system procedures FileOpen, FileCreate, FileRead, FileWrite, FileSeek and FileClose which resides in unit SysUtils.". And so, FileWrite takes longint. – Imobilis Jan 24 '16 at 03:05
  • write pixels[0], and then free the stream to make sure the file is closed (the OS might still be caching). The 16-bit thing is not it, FPC uses sizeint or longint for such things. – Marco van de Voort Jan 24 '16 at 12:48
  • won't this write only 3 bytes ? Btw I am coming back from C. Oh, this worked. But why ? – Imobilis Jan 24 '16 at 16:50
  • @MarcovandeVoort http://4.bp.blogspot.com/-pI2CWxjhGpc/UkRovFX8e3I/AAAAAAAAAZU/bq6zXdU-ecw/s1600/Programmers-Funny-Pictures-Coding-Jokes.jpg – Imobilis Jan 24 '16 at 16:58
  • I needed more room, so added an answer. – Marco van de Voort Jan 24 '16 at 19:28

1 Answers1

1

The first parameter of stream.write is a so called formal parameter, like stream.write(const buf;size:integer) or so.

The compiler takes the address of whatever you pass to it and gives that to the procedure. Because you use an array without borders for the first level (array of array..) it is an dynamic array so a pointer under the hood.

If you pass the array to it, you actually pass the memory location where the pointer is stored. Solution: pass the first element, pixels[0] which is the location of the data.

Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89