Thread: FILE I/O
View Single Post
  #2 (permalink)  
Old August 14th, 2003, 07:50 AM
Olorin Olorin is offline
Authorized User
Join Date: Jun 2003
Location: Gainesville, FL, USA.
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts

My cheap $0.02:
Assuming you know the format of the file (as in your example), you'd also know how many bytes there are from the beginning of the file to the beginning of the field you want to change, right?
For instance, in your example, it seems that the file uses fixed-width fields.
So, you know that "FILE" is at the beginning of the file, and is 4 chars wide, followed by a space, and then (looks like) 18 chars (the file path and the spaces after it), and then the RP090L... code, followed by a date, and then "ENGLISH UNITS"... and so on.. if you know that "ROUTE" always begins on byte X, then you can use fseek to move the file pointer to the Xth byte in the file. Then, of course, you'd have to move forward by 5 bytes (the width of "ROUTE"), plus one more for the sapce... so you might as well fseek to X+6.
Then, I am assuming that the field where "nc 50" is written is fixed-width, you simply write your new Route (i.e. "NC 181"). This will overwrite "nc 50" and one space following that, but that is a safe operation (since the route, as I said, is written in fixed-wodth... Actually, to be safe, you should padd the new Route with spaces to the right, so that if the old Route was longer than the new one, you'd overwrite the whole old route...
And then you're done...

You need to open the file in read & write mode,
and, as I said, I assumed your file has fixed-width fields, to make things easy.
If you don't have the luxury of fixed-width fields, you must parse through the file until you find the spot where you want to write...

Reply With Quote