Hi, is there a way to open a file and load it line by line (each line terminated with 10/13) instead of loading the whole file at once? My program should convert old dbfiles, so it would be nicer to me to read one line at a time, process it and then read the next line. i *could* read the file one byte at a time untill i reach the terminator (10/13), process the data from start to terminator and read the next bytes starting from offset wherever the last terminator was found. But i want to know if there is a line input first before i write that code.
Posted on 2001-02-09 21:28:00 by Fogger
Don't read the data byte by byte: it will be painfully slow. If you want an easy way to read text line by line, load the file into a hidden multiline richedit control. There, you can extract the text line-by-line from the control. If you don't want to depend on the richedit control, you can map the file into memory by using file mapping. You don't have to map the whole file into memory at once: you can do it block by block. It's more efficient than reading the data byte by byte.
Posted on 2001-02-10 07:53:00 by Iczelion
Here's some code I use to load a config file interpting it line by line. Note this was written specifically for 3 lines but you could modify that easily .Data ConfigFile db "Config.ini",0 EndOfLine db 13 .Data? hFile HANDLE ? hMemory HANDLE ? pMemory DWORD ? SizeReadWrite DWORD ? .code LoadConfigFile Proc invoke CreateFile,ADDR ConfigFile,\ GENERIC_READ or GENERIC_WRITE ,\ FILE_SHARE_READ or FILE_SHARE_WRITE,\ NULL,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,\ NULL mov hFile,eax invoke GlobalAlloc,GMEM_MOVEABLE or GMEM_ZEROINIT,MEMSIZE mov hMemory,eax invoke GlobalLock,hMemory mov pMemory,eax invoke ReadFile,hFile,pMemory,MEMSIZE-1,ADDR SizeReadWrite,NULL mov eax, 1 invoke LoadLine Push eax invoke atodw,ADDR buffer mov fullscreen, al Pop eax invoke LoadLine Push eax invoke atodw,ADDR buffer mov ScrnWidth, eax Pop eax invoke LoadLine Push eax invoke atodw,ADDR buffer mov ScrnHeight, eax Pop eax invoke CloseHandle,hFile invoke GlobalUnlock,pMemory invoke GlobalFree,hMemory ret LoadConfigFile EndP LoadLine Proc Push ebx Push ecx Push esi Push edi mov ebx, eax Push ebx invoke InString,eax,pMemory,ADDR EndOfLine Pop ebx sub eax, ebx Push eax lea edi, buffer[0] mov esi, pMemory add esi, ebx dec esi inc eax sub eax, ebx mov byte ptr [0],0 mov ecx, eax MemCpy: mov al, byte ptr [-1] mov byte ptr [-1], al dec ecx jnz MemCpy Pop eax add eax, 3 Pop edi Pop esi Pop ecx Pop ebx ret LoadLine EndP This message was edited by Zadkiel, on 2/10/2001 6:49:49 PM
Posted on 2001-02-10 18:49:00 by Zadkiel
Thanks, i haven't included your LoadLine Proc in my code yet, but i'll give it a try. and Iczelion, Hutch, Thomas and all others: great tutorials, thanks. I've waited with win32 programming for a long time because when i first looked at some code it looked confusing, but i'm starting to like it.
Posted on 2001-02-10 22:47:00 by Fogger