octave-bug-tracker
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Octave-bug-tracker] [bug #63968] fwrite writes incorrect number of byte


From: anonymous
Subject: [Octave-bug-tracker] [bug #63968] fwrite writes incorrect number of bytes
Date: Mon, 27 Mar 2023 10:05:21 -0400 (EDT)

URL:
  <https://savannah.gnu.org/bugs/?63968>

                 Summary: fwrite writes incorrect number of bytes
                   Group: GNU Octave
               Submitter: None
               Submitted: Mon 27 Mar 2023 02:05:19 PM UTC
                Category: Octave Function
                Severity: 3 - Normal
                Priority: 5 - Normal
              Item Group: Incorrect Result
                  Status: None
             Assigned to: None
         Originator Name: Michael Swamp
        Originator Email: mswamp@msrc.com
             Open/Closed: Open
                 Release: 8.1.0
         Discussion Lock: Any
        Operating System: Microsoft Windows
           Fixed Release: None
         Planned Release: None


    _______________________________________________________

Follow-up Comments:


-------------------------------------------------------
Date: Mon 27 Mar 2023 02:05:19 PM UTC By: Anonymous
Seems to be a few bugs with fwrite.  Not only is it writing incorrect data
when you do binary writes with uint8, but it also seems to be writing an
incorrect number of bytes and not reporting a write error.

Run the following script.  It does not report a write error, but when you read
the data back it has a different amount of bytes.

display("Writing File");
ID = tic();
fid = fopen("TestWrite","w");
for i = 1:255
  for j = 1:254
    for k = 1:253
      CNT = fwrite(fid,uint8(k),"uint8");
      if (CNT == 0)
        display(sprintf("Index %d Index %d Index %d Write Failure\n",i,j,k));
        error("Write Error");
      endif
    endfor
  endfor
endfor
WRITE_FP = ftell(fid);
display(sprintf("Wrote %d bytes\n",i*j*k));
display(sprintf("File Pointer %d\n",WRITE_FP));
fclose(fid);
elapsed_time = toc(ID);
display(sprintf("Write took %f seconds\n",elapsed_time));
##
## Read back Data
##
display("Reading File");
ID = tic();
fid = fopen("TestWrite","r");
DATA = uint8(fread(fid,"uint8"));
READ_FP = ftell(fid);
fclose(fid);
elapsed_time = toc(ID);
display(sprintf("Read took %f seconds\n",elapsed_time));
display(sprintf("Read %d bytes\n",length(DATA)));
display(sprintf("File Pointer %d\n",READ_FP));
##
## Read Back Data - Method 2
display("Reading File");
ID = tic();
fid = fopen("TestWrite","r");
DATA2=uint8([]);
for i = 1:255
  for j = 1:254
    for k = 1:253
      [VAL, CNT] = fread(fid,1,"uint8");
      if (CNT == 0)
        READ2_FP = ftell(fid);
        display(sprintf("Index %d Index %d Index %d Read Failure\n",i,j,k));
        display(sprintf("File Pointer %d\n",READ2_FP));
        error("Invalid Read");
      endif
      DATA2(i*j+k) = uint8(VAL);
    endfor
  endfor
endfor
fclose(fid);
elapsed_time = toc(ID);
display(sprintf("Read took %f seconds\n",elapsed_time));
display(sprintf("Read %d bytes\n",length(DATA)));






    _______________________________________________________
File Attachments:


-------------------------------------------------------
Date: Mon 27 Mar 2023 02:05:19 PM UTC  Name: TestWriteFile.m  Size: 2KiB   By:
None

<http://savannah.gnu.org/bugs/download.php?file_id=54532>

    _______________________________________________________

Reply to this item at:

  <https://savannah.gnu.org/bugs/?63968>

_______________________________________________
Message sent via Savannah
https://savannah.gnu.org/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]