i need to write information to a file here is the code im using.
the problem is that this code is process when one of my buttons is clicked and it writes to the my file if i press it once but it appears to keep overwriting the same line when pressed multiple times. so basically ive been only able to write one line of text to a file. so how do i make it so it doesnt overwrite existing text and places new text in instead? smurf
invoke CreateFile, addr TestName, GENERIC_READ + GENERIC_WRITE, FILE_SHARE_READ + FILE_SHARE_WRITE, NULL, TRUNCATE_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL mov hFile1, eax invoke SetFilePointer,hFile1,0,NULL,FILE_BEGIN invoke WriteFile,hFile1,addr OpenText,sizeof OpenText,addr bRead,NULL invoke SetEndOfFile,hFile1
hi smurf, to use flag TRUNCATE_EXISTING will always "clear" the content of your file. If you do not want that, better use OPEN_EXISTING instead. If you want to write a "log file", that is to write at the end of your existing file, use invoke SetFilePointer,hFile1,0,NULL,FILE_END And I think, the statement invoke SetEndOfFile,hFile1 is not needed japheth :)
japheth i changed the code around like you said but it still didnt work. so how can i make it not overwrite the existing line but create a new one? thanks smurf
Use OPEN_EXISTING instead of TRUNCATE_EXISTING. Don't forget to close the file handle after you write a line. But it's more efficient to store several lines in a buffer and write them when the buffer is full.
hi karim. the probelm i have now is that it doesnt goto the next line the new text is written to the same line. how do i make is make a new line to write to? here is my code:
invoke CreateFile, addr TestName, GENERIC_READ + GENERIC_WRITE, FILE_SHARE_READ + FILE_SHARE_WRITE, NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL mov hFile1, eax invoke SetFilePointer,hFile1,NULL,NULL,FILE_END invoke WriteFile,hFile1,addr EditText,sizeof EditText,addr bRead,NULL invoke CloseHandle,hFile1
Hi smurf, if I got you right then your problem is to "simulate" the pressing of return-key in order to place the CD/LF into the file. Right? Okay, then you just need to do this:
Just do this after you wrote your line and voilá you are/should be in the next line. I hope there is nothing wrong with the code in here (the server was busy so I just hurried to write). Stefan P.S. I never really understood what this "addr bRead" is for. Can someone please explain this?
.DATA NeueZeile db 0Dh,0Ah ... .CODE ... INVOKE WriteFile,hFile1,addr NeueZeile,2,addr bRead,NULL ...
thanks Stefan that worked great. smurf
now on to my next problem. my file is loading into a listview and when i delete a string from my listview i need the same string to be deleted from my file. how would I go about deleting a specific string in my file? any ideas? thanks smurf This message was edited by smurf, on 6/21/2001 5:39:12 AM
Hi smurf, seems to me that we are kind of working into the same direction. I created an editor for my guestbook database some days ago and it does exactly what you need. I must admit that the saving, deleting of the file is not at runtime but it saves on click. Maybe it is not the best way it is done but it works. If you want you can have a copy. I would upload it to my server but I need to redo my layout so I am not doing anything with my website at the moment. Stefan
I think Stephan's approach is more efficient. It's faster to work with a buffer and store it to disk when the user is done. For example, you could allocate your lines at runtime (with HeapAlloc), and store the pointers in an array. When the user removes a line, you simply delete the pointer. Then, when the user doesn't need the listview anymore, you store the lines on a file.
hey Stefan im very interested in your code. please send to firstname.lastname@example.org thanks smurf
all you have to do is memorymap your file. read every line in your listbox ... use some extra space or the stack as index holder. if you want to delete a line you have to find its index - go to the memory position and copy all stuff after the line to the position before so that the line is completly deleted... update indexes and everything is fine (note that all changes are directly reflected to your file so you not have to use write or readfile functions... just make use of the setfilepointer and setfileend functions to mark the new EOF). good luck