Hi,

Is there an api to read the number of lines in a file or an api to read from a particular line in a file? If no, do I have to copy the data in the file to a buffer and then scan for CRLF? Or should I read the file to a richedit and get file line from there?
Posted on 2002-09-20 03:02:17 by roticv
There is no API that I know of to count line numbers. I don't think there will be, the API would need to scan through the file in some way, which would make it the mother of all dog-slow functions. If its left in the programmers hands, then a much more suitable method of counting can be implemented (depending on the use of the file afterwards, and so forth).

You could load it into a RichEdit, but unless you actually need to use a RichEdit for display of that same data afterwards, then you'll be wasting functionality (the setup costs of the RichEdit, and the data loader & parser will most likely do processing you won't need for simple line counting). But if you're feeling lazy, and the code doesn't need to be fast (it may even be smaller because the code will all be in a system DLL somewhere)...

Mirno
Posted on 2002-09-20 03:09:01 by Mirno
I don't see why it would be the job of an API function and using richedit for that sounds like overkill. :)
It is very easy to do:

You open your file, your read your file and compare each byte of your file with the LF character, and if it is equal, you increment your counter...

For file handling you have three ways :

loading the whole file in a HeapAlloced/VirtualAlloced buffer (the fastest, but may consume lot of memory if your file is big)
reading each byte from the file at a time (secure, no need of memory other than a byte, but can be very slow)
using memory mapping (no need of much memory, you can access your file like it was fully loaded in a buffer, but may be a little bit slower than the first solution)

In your case, I would recommend memory mapping if you want to deal with moderatly big files and you want to save some memory...
Posted on 2002-09-20 04:45:28 by JCP
Yeah, I thought abt that. However when typing the post, the idea on richedit pop out of my head. That's why i added that brilliant idea :grin: hehe... i did thought of using heapalloc and not memorymapping.

Thanks anyway fellows
Posted on 2002-09-20 07:55:45 by roticv
Hi,

I got a qn. What did i do wrong with the code? I'm trying to scan for value of LF



mov esi,pbuffer
mov ecx,filesize
loop1:
lea edi,[eax+esi]
cmp edi,0Dh
jne contiuneloop
inc ebx
contiuneloop:
inc eax
loop loop1
mov nooflines,ebx

Posted on 2002-09-20 23:59:36 by roticv
Hi,






lea esi,pbuffer
mov ecx,filesize
xor ebx,ebx
xor edx,edx

loop1:
lodsb
cmp al,0Dh
jne contiuneloop
inc ebx
contiuneloop:
inc edx
dec ecx
jnz loop1

mov [nooflines],ebx
mov [lfindex],edx


have nice days!
Posted on 2002-09-21 20:49:03 by CYDONIA
Hi,

doesn't loop label = dec ecx, cmp ecx,0 jnz label?
Posted on 2002-09-22 00:41:16 by roticv
But I read somewhere that loop is slower than doing it the longer way...
Posted on 2002-09-22 03:38:18 by scientica
Hi,

Just a qn, what is the difference between test eax,eax and cmp eax,eax?
Posted on 2002-09-22 06:45:45 by roticv
test eax, eax
preforms a logcal and with eax (and eax) without saving the result in any register, but it uppdates the flags (wich are affected by an and operation).

cmp eax, eax
compares all flags (overflow, signed, etc)

So, cmp eax does more checking than test; at least thats how I have read it.
Posted on 2002-09-22 07:01:20 by scientica
Hi roticv,



...
dec ecx
jnz loop1




If ECX register is 0, CPU setting zero flag. JNZ ( jump not zero) control for zero flag.

have nice days
Posted on 2002-09-22 18:05:04 by CYDONIA
Hi,

Does that mean that i do not need to do cmp ecx,0 jnz label?
Posted on 2002-09-23 08:44:58 by roticv
roticv:

test performs a logical and but discards the results while updating the flags.
as used here:test eax,eax it is used to test for zero/nonzero

on the other hand, cmp performs a subtraction discards the results and updates the flags... it can therefore be used to test for any relation.

you wouldn't normally do cmp eax,eax since this will always result in equality...

here's a simple procedure which should do the job. Just pass to it a pointer to the buffer and the length of the buffer



LineCount proc _lpBuffer:dword,_dwSize:dword
;counts the number of lines
;_lpBuffer ...pointer to buffer with data
;_dwSize ...size of buffer in bytes
;returns number of lines in eax
xor eax,eax ;will hold the line count
mov edx,_lpBuffer
mov ecx,_dwSize

inc_line:
inc eax
next_char:
dec ecx
cmp byte ptr[edx+ecx],0AH ;line feed
je inc_line
test ecx,ecx
jnz next_char
ret
LineCount endp

Posted on 2002-09-23 10:12:05 by MArtial_Code
Hi,

I am just going to create a magic ball 8 program that reads the results it goin to display in a file. Any suggestions to add to it? Such a program would be useful to anyone of us at time. :grin: Thus i hope i am doing everyone a favor ;)
Posted on 2002-09-24 03:08:19 by roticv
Hi,

I have encountered a problem while coding a proc that reads the value in the line. Help needed. Most of the time the proc works perfectly okay, but sometimes it crashes. Help needed thanks



GetLine proc uses edi ecx edx esi lpbuffer:DWORD,line,lpstorebuffer
mov esi,lpbuffer
mov edi,lpstorebuffer
xor edx,edx
dec edx
loopa:
inc edx
cmp BYTE PTR[esi+edx],0Ah
jne loopa
inc_ecx:
inc ecx
cmp ecx,line
jne loopa
add esi,edx
inc esi
loopb: ;store into buffer
cmp BYTE PTR[esi], 0Ah
je finishloop
movsb
jmp loopb
finishloop:
xor eax,eax
ret
GetLine Endp



**The program would generate ACCESS_VIOLATION sometimes and crash on the following code.


cmp BYTE PTR[esi+edx],0Ah


**PS optimisation is welcomed ;)
Posted on 2002-09-26 10:31:10 by roticv
You may want to pass another parameter to the function which is the buffer size
and make sure that you don't read past the end of your buffer/file in your loop in
case you don't have the last line terminated with a carriage return.
Posted on 2002-09-26 11:10:06 by goofee
I solved that problem, I just forgot to create a special case when line = 0. :grin:

Thanks anyway
Posted on 2002-09-27 06:52:16 by roticv