Openfile problem

Blitz3D Forums/Blitz3D Beginners Area/Openfile problem

Farflame(Posted 2006) [#1]
Is Openfile bugged? I need to open a file which has 7 strings in it. I need to ignore the first two strings, and replace the remaining five. My plan was to use openfile, use readline to read off the first two strings and put the pointer to the correct position, and then use writeline to replace the remaining 5. But the writeline command just seems to do nothing.

The only other way around it I can see it to open the file with readfile, read and store the 2 strings, close the file, open it again with writefile and then put all 7 strings back in and close it again. Seems like a very long-winded way around it.

Am I doing something wrong, or is openfile just bugged?


Beaker(Posted 2006) [#2]
You can't use ReadLine() to read strings (unless they aren't actually strings, but lines :). Try ReadString() instead.

Also, strings (and lines) aren't a fixed length, so trying to replace them with WriteLine (or WriteString) will probably overwrite more (or less than) one line/string. You are better off creating a new file (or overwriting the old one) and copying the parts you still want and your new parts to it.

Confused?

PS. OpenFile() isn't bugged, but you should use ReadFile() if all you want to do is read it. OpenFile lets you read and write to a file (without closing it).


Farflame(Posted 2006) [#3]
That's odd, I've been using Readline and Writeline to read and write strings to my files for ages, seems to work fine.

I guess in this case I will just replace the whole file, at least it works. :)


Sir Gak(Posted 2006) [#4]
Generally, I don't use OpenFile, because ReadFile and WriteFile give me all the functionality I need. Are you using the FilePos and SeekFile commands in conjunction with OpenFile, to find your way around in the file? If not, then from what I understand from the Help docs on the command, your use of OpenFile is incorrect.

This command opens the designated file and prepares it to be updated. The file must exists since this function will not create a new file.

By using FilePos and SeekFile the position within the file that is being read or written can be determined and also changed. This allows a file to be read and updated without having to make a new copy of the file or working through the whole file sequentially. This could be useful if you have created a database file and you want to find and update just a few records within it.


I hope this helps.