Random access problems.
Thx
[3 byte] By [
pursegcn] at [2007-11-11 10:07:14]

# 1 Re: Random access problems.
The "static" keyword may help u in case of one time storing , I mean from one only execution .. but if u want to use this number several time then u may save it in the top of your file , or maybe in a new file and every exectution of this function at the begining you get this number, use it , increase it with one and save it again [hence the next read for it will be the new number] .. same thing could be done using registry if u have experience using it .
Amahdy at 2007-11-11 20:59:28 >

# 2 Re: Random access problems.
for random access files you have to write the entire file with blank records up to the max user number. Then as you get records, you seek that position (record size * number) and write the record in that spot. When they query, they provide the record number and you seek that position, read it, and spit it back to the user. If they seek a blanked record you can display it or note that its invalid ID number and message the user...
jonnin at 2007-11-11 21:00:28 >

# 3 Re: Random access problems.
To store it at the top of the file you need to define a special file structure: the fist x bytes contain a header, which specifies how many records the file contains, the size of a single record and perhaps other flags (text/bin etc.). The remaining data are a sequence of records. Whenver you access such a file, you first read the header and then according to the information in the header, you process the rest of the file.
Danny at 2007-11-11 21:01:32 >

# 4 Re: Random access problems.
Yea Danny has told u the good method which need a knowledge about "filestructure" that's why I told u : "or maybe in a new file" .. otherwise if your file isn't too long you may make the traditional method which could be done using one of those gates :
-get the content of the whole file in a big variable, store in the file "output-method" the first line, then finally restore "append-method" the variable container in the file back.
-make a temporary comy of the file , clear the original file and insert in it the first line , append what exists in the temporary file to the original file , delete the temporary file !!!
... this method is very old , it works but not effective .
Now I can tell u a new method to insert this number : put it dynamically at the end of your file :
*each time the file is opened for appending data , first you must read the last line [which contains the number record] , delete the last line , and append your data then finally rewrite the record at the end (this is why I called it dynamically) ...
**just you need a good technice to read the last line but it's easy .
Amahdy at 2007-11-11 21:02:33 >

# 5 Re: Random access problems.
There is another technique for storing a variable number of records in the file. Use a fixed record size. When you've opened the file, first calculate its size by seeking to its end. Then divide the file size by the size of a record. The result is the number of records currently stored in the file. You can then access random records by calculating tehir relative offsets. For example, the 100th record is at offset 99*sizeof(record) from the beginning of the file
Danny at 2007-11-11 21:03:26 >
