How do you write to a text file? Also, how do you read from one? Some examle code would be greatly appreciated.

Thanks (I'm a n00b),
psyphen
Posted on 2003-06-11 21:31:06 by psyphen
Open the file using CreateFile then write to it using WriteFile. Make sure you terminate the lines with 0D0A (CR/LF).
Posted on 2003-06-11 21:39:12 by donkey
Yes, but how do you get the text from an already-written-to file? I want to write the file with one program, and have another open, read, and display the text in the text file.
Posted on 2003-06-11 21:46:43 by psyphen
Open the file with GENERIC_READ access and read the file with ReadFile. You have to do the line parsing by yourself (i.e. find the CRLF's) there are examples on the board for this type of thing.
ReadFileLines proc uses esi edi ebx

LOCAL Char[2] :BYTE
LOCAL nBytes :DWORD
LOCAL nRead :DWORD

lea esi,Char
mov edi,OFFSET buffer
xor ebx,ebx

ReadLine:
inc nRead
invoke ReadFile,hFile,esi,1,ADDR nBytes,NULL
cmp nBytes,NULL
je @F
mov al,[esi]
cmp al,0Dh
je @F
mov [edi],al
inc edi
inc ebx
jmp ReadLine
@@:
mov [edi],BYTE PTR 0
lea edi,buffer
invoke SetFilePointer,hFile,1,NULL,FILE_CURRENT
inc nRead

; the file line and it's length are in buffer and ebx respectively

; You would normally call a callback routine here to process the line

cmp nBytes,NULL
je @F
xor ebx,ebx
jmp ReadLine
@@:


ret
ReadFileLines endp
Posted on 2003-06-11 22:06:16 by donkey
If you don't have to change the file size you can use Memory Mapped file access. Read tut 12 and 13
Posted on 2003-06-12 02:12:25 by greenant