flat assembler
Message board for the users of flat assembler.

Index > Windows > Read string and save to file

Author
Thread Post new topic Reply to topic
lukasz3t



Joined: 23 Jan 2014
Posts: 2
lukasz3t 23 Jan 2014, 14:19
I'm trying write program which read string from keyboard and save it to file.
The code is incomplete, especially writing to file. How should I use fprintf? Reading data from keyboard is correctly?

Code:
format PE CONSOLE
    entry main
    include 'e:\fasm\include\win32a.inc'
     
    section '.data' data readable writeable
            filename db 'test.txt', 0
            type db 'w', 0
            text db ?
            text_size = $ - text

    section '.code' code readable writeable executable
            proc main


                            cinvoke scanf
                            mov [text], al
                            cinvoke printf, text
              .save_to_file:
                            cinvoke fprintf, filename, text     ;??

                            cinvoke getch
                            xor eax, eax
     
                    .end:
                        ret
            endp
     
    section '.idata' import data readable writeable
            library msvcrt, 'msvcrt.dll'
             
            import msvcrt,\
                    fclose, 'fclose',\
                    fgetc, 'fgetc',\
                    printf, 'printf',\
                    getch, '_getch',\
                    scanf, 'scanf_s',\
                    fprintf, 'fprintf'     
    
Post 23 Jan 2014, 14:19
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1678
Location: Toronto, Canada
AsmGuru62 23 Jan 2014, 16:29
Where is 'fopen()'?
Code:
              .save_to_file:
;use fopen() here ... 
                            cinvoke fprintf, filename, text     ;?? 
;use fclose() here ... 
    
Post 23 Jan 2014, 16:29
View user's profile Send private message Send e-mail Reply with quote
lukasz3t



Joined: 23 Jan 2014
Posts: 2
lukasz3t 23 Jan 2014, 18:08
program is not responding

Code:
format PE CONSOLE
    entry main
    include 'd:\fasm\include\win32a.inc'
     
    section '.data' data readable writeable
            filename db 'test.txt', 0
            type db 'w', 0
            text db ?
            text_size = $ - text

    section '.code' code readable writeable executable
            proc main


                            cinvoke scanf
                            mov [text], al
                            cinvoke printf, text
              .save_to_file:
                            cinvoke fopen, filename, type
                            cinvoke fprintf, filename, text     ;??
                            cinvoke fclose, filename

                            cinvoke getch
                            xor eax, eax
     
                    .end:
                        ret
            endp
     
    section '.idata' import data readable writeable
            library msvcrt, 'msvcrt.dll'
             
            import msvcrt,\
                    fopen, 'fopen',\
                    fclose, 'fclose',\
                    fgetc, 'fgetc',\
                    printf, 'printf',\
                    getch, '_getch',\
                    scanf, 'scanf_s',\
                    fprintf, 'fprintf'
    
Post 23 Jan 2014, 18:08
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 23 Jan 2014, 18:35
lukasz3t,

How do you think, what's the prototype of scanf()?
Post 23 Jan 2014, 18:35
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1678
Location: Toronto, Canada
AsmGuru62 23 Jan 2014, 20:08
lukasz3t,

Please take a look at every function's parameters.
You have used 'filename' in a few places in a wrong way.
This is a good point to begin:
http://www.cplusplus.com/reference/cstdio/

Also, to read a string -- you need a buffer to read it into.
I see you have this:
Code:
text db ?
    

This is a 1 byte buffer -- you need more room for a string, like:
Code:
text rb 80   ; 80 bytes of room
    

scanf() is also not used properly, as already mentioned.
Take a look at its parameters.
Post 23 Jan 2014, 20:08
View user's profile Send private message Send e-mail 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.