bugGNU Octave - Bugs: bug #62649, fwrite function does not work...

 
 

bug #62649: fwrite function does not work properly with some value

Submitter:  Aurelien Chiron <ach411>
Submitted:  Sat 18 Jun 2022 02:46:34 AM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Incorrect Result
Status:  Invalid / Not an Octave Bug Assigned to:  None
Originator Name:  ach411 Open/Closed:  * Closed
Release:  * 6.4.0 Operating System:  * Microsoft Windows
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Mon 20 Jun 2022 02:58:25 PM UTC, comment #2: 

Works for me on Windows when opening the file in binary mode:

fid = fopen("test_values", 'wb');
fwrite(fid, 2559, "uint16");
fwrite(fid, 2560, "uint16");
fwrite(fid, 2561, "uint16");
fclose(fid);


In hex viewer:
ff 09 20 0a 01 0a 

Fwiw, Matlab R2022a on Windows does the same (conversion of LF to CRLF) for the original example.

Closing as invalid.

Markus Mützel <mmuetzel>
Group administrator
Sat 18 Jun 2022 03:09:42 AM UTC, comment #1: 

Why are you opening the file in text mode?

What you want to write is:


od -c test_values
0000000 377  \t  \0  \n 001  \n
0000006


but in the text mode (On Windows) every linefeed (\n) will get
carrige return \r added. So you are getting:

od -c test_values
0000000 377  \t  \0  \r  \n 001  \r  \n
0000010


Dmitri.
--


Dmitri A. Sergatskov <dasergatskov>
Sat 18 Jun 2022 02:46:34 AM UTC, original submission:  

This code snippet shows the issue. It should write 3x unsigned 16-bit values (6 bytes) into a file. Instead, on my system, I end up with 8 bytes. The first 16-bit value appears to be correct but not the second one, nor the 3rd one.


fid = fopen("test_values", 'wt');
fwrite(fid, 2559, "uint16");
fwrite(fid, 2560, "uint16");
fwrite(fid, 2561, "uint16");
fclose(fid);


The resulting binary file "test_values" viewed by an Hexadecimal viewer:
ff 09 00 0d 0a 01 0d 0a

Windows being little endian, the first value written is correct: 0x09ff. However, the second value should read 0x0a00, instead we have 3 bytes and it looks like the byte 0x0d was incorrectly inserted in between 0x00 and 0x0a. Same thing is true for the 3r value.

Aurelien Chiron <ach411>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by mmuetzel (Posted a comment)
  • -email is unavailable- added by dasergatskov (Posted a comment)
  • -email is unavailable- added by ach411 (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 2 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2022-06-20 mmuetzel StatusNone Invalid / Not an Octave Bug
        Open/ClosedOpen Closed

    Back to the top

    Powered by Savane 3.13-f8d8.
    Corresponding source code