# Creating a Random access file Troubles

## Recommended Posts

Poigahn    598
Need help with the proper math for a random access file on the harddrive. I need to create a list of words that are 18 charactors long ( Single Words ) Instead of doing a sequential read/write file. I want to use random access so that when I add a word I can alphabetize it when adding to it. What would be the proper seekfile read write method.

SeekFile([i]filenum,((Fileposition * 18)-18)[/i])
Word$= Readstring([i]filenum[/i]) if (Myword < Word$ ) Then
SeekFile([i]Filenum,((FilePosition * 18) - 18[/i]))
writestring([i]filenum,MyWord[/i])
MyWord = WordString
End If
FilePosition ++
Loop

Not using actual code just psuedo coding above

##### Share on other sites
SiCrane    11839
Did you try implementing your pseudocode? Did it work? If not, then what errors or incorrect behavior did you observe?

##### Share on other sites
Nypyren    12063
I suspect the underlying problem is that when you write at a position in a random access file, you *overwrite* what is already there. In order to insert, you have to shift the entire remainder of the file forward as well.

##### Share on other sites
Poigahn    598
I am not certain if I am Overwriting, Under-Reading or what ? My fixed String\$ length is 18 charactors. I know the first element starts at 0 - But is the anything additional that gets written to a file that I need to account for or will the above math work ?
I have coded the routine. Wrote 6 TextStrings to the diskdrive, but I can't properly read then.
Does Seekfile Only work with binary files ?
Will seekfile Also work on textfiles if each string is the samelength ? ( Adding spaces to shorter words to get to the length of 18 )

##### Share on other sites
SiCrane    11839
How can we know how SeekFile works when you haven't mentioned what programming language, operating system or I/O library you are using? Also "I can't properly read then" isn't an adequate error description. What [i]exactly[/i] goes wrong when you read the file? Did you try opening your file in something like a hex editor to see if it was written properly? And since you now have actual code instead of pseudo-code it would probably help if you showed what you actually wrote. This includes both writing and reading.

##### Share on other sites
Poigahn    598
What went wrong was 2 things. I could write the first data string with a fixed length of 18 charactors. When I try writing the next string by using SeekFile to position the pointer it would either be to short or to long ? Not Certain. Subsequently when I would try to read then Third String out of 5 and compare it to an identical string to make certain I war reading what I was writing, I would no get a match. So I did not know if the math calculation to position the pointer was off and by how many elements.
I Have since solved it by opening the file as a sequential read, reading in each string, The identifying the string size to determine how it was written to the disk drive and then adjusting the math accordingly.

In the end , Problem Solved