flat assembler
Message board for the users of flat assembler.

Index > Windows > problem with fwrite

Author
Thread Post new topic Reply to topic
magicSqr



Joined: 27 Aug 2011
Posts: 105
magicSqr
Hi,

I'm trying to output raw dwords to a file but if the dword contains the byte 0x0A then it is being treated as end of line and a CR 0x0D is being inserted.

i.e.

Code:

format PE console
entry start

include '%fasminc%\win32a.inc'

section '.text' code readable executable

    start:
        cinvoke fopen, fileName, fileWrite
        mov     [fileHnd], eax
        mov     [num], 0x00030AF3
        cinvoke fwrite, num, 4, 1, [fileHnd]
        cinvoke fclose, [fileHnd]
        invoke ExitProcess, 0

;**************************************************************************************************
section '.data' data readable writeable

fileWrite       db "w", 00
fileName        db "tmp.dat", 00
fileHnd         dd ?
num             dd ?

;**************************************************************************************************
section '.idata' import data readable writeable

library msvcrt, 'msvcrt.dll',\
        kernel32, 'kernel32.dll'

import  msvcrt,\
        fopen, 'fopen',\
        fclose, 'fclose',\
        fwrite, 'fwrite'

include '%fasminc%\api\Kernel32.inc'
    


I want the above to produce a 4-byte file containing F3 0A 03 00
but I get a 5-byte file with F3 0D 0A 03 00

How can I get the output I want ?

Thanks
Post 09 Jul 2012, 20:09
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1412
Location: Toronto, Canada
AsmGuru62
Replace:
Code:
fileWrite       db "w", 00    

with:
Code:
fileWrite       db "wb", 00    

Because what you need is a binary write, and by default fopen uses text conversion, where it replaces 0Ah bytes with 2 bytes for CF-LF.

Ref:
http://www.cplusplus.com/reference/clibrary/cstdio/fopen/
Post 09 Jul 2012, 20:20
View user's profile Send private message Send e-mail Reply with quote
magicSqr



Joined: 27 Aug 2011
Posts: 105
magicSqr
That's great. Easy when you know how Wink

Thanks AsmGuru
Post 09 Jul 2012, 20:33
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.