Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2007-10

fpwrite buffers issues

5 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

ik

Hello List,

I'm trying to use fpwrite (from UnixBase). It seems that on my first use it will wrtie the full content of the buffer, however on the second write of the buffer, it will always write a small portion of the buffer no matter the real size of it...
On an example that is written in C (that my code is based on), the function always write the full content of the buffer to that device.

here is a small POC of my first attempt:

Const
  BUFFER_LEN = 2048;
Buffer : array[0..BUFFER_LEN -1] of smallint;

My usage of fpwrite is as follows:

read_content := read_content_from....(, MAX_BUFFER);
while (read_content > 0) do
 begin
    write_count   := fpwrite(fd, @Buffer[0], buffer_size * sizeof(buffertype));
    read_content := read_content_from....(, MAX_BUFFER);
 end;

Then I tried to do the following code, that continue to write the buffer content until nothing more to write, but it reads wrong data:

start_count  := 0;
output_count := readcount * sizeof(cshort);
read_content := read_content_from....(, MAX_BUFFER);
while (read_content > 0) do
 begin
   while (write_count > 0) do
     begin
       write_count := fpwrite (fd, @Buffer[start_count], output_count);
       if (write_count > 0) then
          begin
            dec (output_count, write_count);
            inc (start_count, write_count);
          end ;
    end;
   read_content := read_content_from....(, MAX_BUFFER);
end;

I'm using ubuntu Linux amd64 using FPC 2.2.0

Any idea what I'm missing or doing wrong, and how to do it better ?

Thanks,
Ido

Reply by email

Vincent Snijders
to ik
ik wrote
Hello List,

<snip>

Show quoted text 18 lines
Then I tried to do the following code, that continue to write the buffer content until nothing more to write, but it reads wrong data: start_count := 0; output_count := readcount * sizeof(cshort); read_content := read_content_from....(, MAX_BUFFER); while (read_content > 0) do begin while (write_count > 0) do begin write_count := fpwrite (fd, @Buffer[start_count], output_count); if (write_count > 0) then begin dec (output_count, write_count); inc (start_count, write_count); end ; end;
// this seems missing:
	start_count  := 0;
	output_count := readcount * sizeof(cshort);
Show quoted text 5 lines
read_content := read_content_from....(, MAX_BUFFER); end; I'm using ubuntu Linux amd64 using FPC 2.2.0

Vincent

Reply by email

ik
to Vincent Snijders

Hi Sorry for the delay in responding

Vincent Snijders wrote 27 lines
ik schreef: > Hello List, <snip> > > Then I tried to do the following code, that continue to write the > buffer content until nothing more to write, but it reads wrong data: > > start_count := 0; > output_count := readcount * sizeof(cshort); > read_content := read_content_from....(, MAX_BUFFER); > while (read_content > 0) do > begin > while (write_count > 0) do > begin > write_count := fpwrite (fd, @Buffer[start_count], output_count); > if (write_count > 0) then > begin > dec (output_count, write_count); > inc (start_count, write_count); > end ; > end; // this seems missing: start_count := 0; output_count := readcount * sizeof(cshort);

Well it was missing from my PoC :) but the problem still exists. I'm attaching the full program (it goes to libsdnfile).

Show quoted text 8 lines
> read_content := read_content_from....(, MAX_BUFFER); > end; > > I'm using ubuntu Linux amd64 using FPC 2.2.0 > Vincent

Thank you,
Ido

Reply by email

Vincent Snijders
to ik
ik wrote
Hi Sorry for the delay in responding
Some remarks at first glance:
	for m := 0 to readcount do
Should be:
	for m := 0 to readcount-1 do

It would better to use either cshort or smallint.

I think fpread and fpwrite expect sizes in bytes and not in cshorts (or is a cshort 1 byte).

Vincent

Reply by email

ik
to Vincent Snijders
Vincent Snijders wrote 10 lines
ik schreef: > Hi Sorry for the delay in responding > Some remarks at first glance: for m := 0 to readcount do Should be: for m := 0 to readcount-1 do It would better to use either cshort or smallint.

Thanks that solve most of the problems. Back to learn the new issues that I found :)

I think fpread and fpwrite expect sizes in bytes and not in cshorts (or is a cshort 1 byte).

It's ssize_t and it's longint type...

Vincent

Thank you for the help

Ido

Reply by email