News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Reading a File. Backwards.

Started by htek, August 19, 2005, 01:22:01 PM

Previous topic - Next topic

htek

hey,

It's been a while. Anyway...

I'm writing an application which will require sometimes reading a file backwards. I thought about part of the file i need to read backwards into a buffer and using lodsb... but then sometimes, I don't know how much of the file i need to read backwards (e.g. read backwards until I get to a certain charachter, or sequence of charachters).

So the question is this:

What would be the best way to do this? Currently (without copy and paste - code's on my laptop) i'm using the following procedure, with SetFilePointer and ReadFile:

SET FILE POINTER BACKWARDS 1
READ FILE, 1 CHAR
SET FILE POINTER BACKWARDS 1

so our file pointer is in effect, decremented, while i've got the charachter to do my cmp's and so on. is there an easier way? is there a more efficient way?

thanks in advance,


h

Tedd

Ooooouch. :dazzled:
A better way would be to read a block of the file (at the end, obviously) using ReadFile, and then scan through that while it's in memory. If you do get to the start of that block, then you can always read another.
If you're worried about the extra time taken to read a block, as apposed to a few bytes - don't. Files will always be read in blocks anyway, so whether you read 1 byte or 200, the time will be the same. Obviously this doesn't scale up to reading MB of files, but it depends on the size of the block. I would say that reading in 4kB blocks is a nice number :wink
No snowflake in an avalanche feels responsible.

hutch--

Two choices as far as I can see, read it into a buffer and read it backeards with a dedicated algo or reverse it in the buffer and read it forwards.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

P1

htek,

About How big of a file?

Is it Binary or Ascii or Both?

What you are trying to accomplish?

What do you see being the average system trying to perform this task?

Regards,  P1  :8)

ThoughtCriminal

If I'm not mistake ReadFile will also give you the number of bytes read and the address where it was loaded.

Add the bytes read to the start address (might need a -1), and you should have the address of the last byte of the file.

Write a loop that decremants that address and us it as a pointer to into the file.

Dont bother with file pointers, just get a pointer to where you want to start.  Decrement or increment by 4 for dword or 1 for byte.

Tedd

Quote from: ThoughtCriminal on August 19, 2005, 02:57:48 PM
If I'm not mistake ReadFile will also give you the number of bytes read and the address where it was loaded.

You give the ReadFile function the address and length - but yes, you'll still know where the end of the block is (address+length-1)
No snowflake in an avalanche feels responsible.

htek

Quote from: P1 on August 19, 2005, 02:56:54 PM
htek,
About How big of a file?
Is it Binary or Ascii or Both?
What you are trying to accomplish?
What do you see being the average system trying to perform this task?
Regards,  P1  :8)

1] About How big of a file?

Well, the application itself is a kinda scripting language thingymabob - you know, one of those programs to take a script and do repetitive actions on a socket or a file, with the action maybe modifying itself a little bit each time. Thus, I wouldn't expect the files to be over 1KB (or i'd be worried about the mental health of the person who wrote the script!!)

2] Is it Binary or Ascii or Both?

Completely ASCII. I've got a representation for binary charachter's, where the number-value of the charachter is stored in brackets (there's an idea for you guys. after a while. you really get used to the notation.... i think it's neater than C's in large, bracket-filled programs. give it a try sometime!)

3] What you are trying to accomplish?

See #1

4] What do you see being the average system trying to perform this task?

The "minimum average" system should generally be a system capable of running XP and CPU, say.... P3? 1GHz? In this project, I've tried to be general, so you _CAN_ even run it on, say, a Win95 Pentium 1 75 with 16MB RAM system (tested on my old box :)) but... the average should be what any of you guys would use.

I've done my research into this topic, and I think, considering the situation, I have a way - however painful for the code. Being metacode-based, and (on a challenge from a friend) not being able to use any intermediate code or pseudo-assembled code (i.e. the program must be able to change itself and execute the change during execution itself, without relying on bytecode), i think the setting the file pointer back one would be the best way - since I can't be sure of the size of the file, and I don't want to allocate a buffer and read the file in (sometimes, due to metacode, the file can grow quite large, quickly). Besides, I already have a 30K buffer allocated (i've incorporated inline Brainf*ck. Take that GCC... you think inline assembler's good.. pfft :P). So.... I think i'll just leave it to set the file pointer backwards - unless there's some secret ReadFile API argument that you guys are hiding from me...

Thanks all,


h