Gettin' no love from WriteLine
Blitz3D Forums/Blitz3D Beginners Area/Gettin' no love from WriteLine
| ||
I'm working with plain text files that I need to read a bunch of stuff out of, and write some other stuff back in. In order to refresh my memory about how such things work, I threw together a lil sample proggy just doing the basics (as one does). Trouble is, it's got me a lil flummoxed. It's supposed to just (essentially) create and write to a file (using WriteFile and WriteLine), close the file, then reopen it using OpenFile and WriteLine to it some more. At this stage, I'm not concerned whether it overwrites stuff, or needs to be the same length, or anything like that... but at the moment, it just seems to ignore me. What obvious and n00b-esque thing am I missing? (bear in mind I'm only interested in working with complete lines - the sort of thing ReadLine and WriteLine do - so I don't expect anything like any of the other reads and writes (for ints, bytes etc) are likely relevant) Const testFileName$ = "testfile.txt" Global numLines, textLine$, i, temp$ Global filehandle filehandle = WriteFile (testFileName$) If Not filehandle Then RuntimeError "File could not be created" Restore startData Read numLines For i = 1 To numLines Read textLine$ WriteLine filehandle, textLine$ Next ; i CloseFile filehandle filehandle = OpenFile (testFileName$) If Not filehandle Then RuntimeError "File could not be opened" For i = 1 To 3 temp$ = ReadLine$ (filehandle) ; read through a couple of lines temp$ = ReadLine$ (filehandle) ; just to move through the file a bit Next ;------------------------------------------------------------------------- ; This line doesn't seem to do anything. This is what I'm asking about. WriteLine filehandle, "This is the line that gets added later. Or not." ;------------------------------------------------------------------------- CloseFile filehandle End .startData Data 6 Data "This is a test to see what things do. This is line one." Data "This is line two." Data "This is line three. A few more to come." Data "This is line four." Data "This is line five - the penultimate line." Data "This is line six. The end." |
| ||
This .. filehandle = OpenFile (testFileName$) Should be filehandle = ReadFile (testFileName$) |
| ||
It seems like the program doesn't know where to write in the file and does nothing. If you use SeekFile to move to a specific location the WriteLine will work as expected. Bizarrely, if you just call FilePos() you can then write at the end of the file. In your program just add one line so you have IgnoreThis = FilePos(filehandle) WriteLine filehandle, "This is the line that gets added later. Or not." I used FilePos recently when I wanted to write a text file with many words per line, but writing only one word at a time. The problem was that WriteLine adds a carriage return and line feed. So I ended up doing this after writing each word: SeekFile filehandle, FilePos(filehandle) - 2 which backs up two characters so the next WriteLine will simply overwrite the previous CR-LF. |
| ||
Stevie G: Ta, but I wanna write to that file too. I believe ReadFile opens it for reading only. Writefile creates it (and/or REcreates it, thus wiping what's in there already). Openfile is the only command that allows reading and writing whilst preserving the contents. Floyd: The original cunning plan was to move through the file one line at a time, and when I got to where I wanted to overwrite/insert a complete line at the position I'm at. I've since realised that's not going to happen, and so have come up with another more cunning plan. I persisted with this enquiry though because it had me bejiggered that it didn't do anything, nor throw an error. Seeing you say It seems like the program doesn't know where to write in the file and does nothing. makes me feel a little better at least, since generally speaking that sort of thing isn't supposed to happen. :o)Thanks for the CR/LF removal tip though - that could prove handy down the line. |
| ||
Another solution to the CRLF thing is to use ReadString/WriteString. The trick with that is to store a byte/int/whatever before the string, so you know how long the string is when reading it back. |
| ||
Isn't that what WriteString does anyway? From the docs: Each string is stored in the file as a 4 byte (32bit) integer followed by the characters that form the string. The integer contains the number of characters in the string, i.e. its length. |