- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
My program creates a file of type sequential formatted that look similar to this:
$KEYWORD
word1
word2
:
wordlast
$END
$COMMENTS
This is a free-form section containing text of
various user comments
$END
The file is then closed. In a subsequent run of the same program, it needs to open this file and edit it to delete the keyword list with a new list, which can be either longer or shorter than the existing list. How can I do this?
My strategy was to open the file, read records until '$KEYWORD' is found, then delete records until '$END' is found, then backspace, then write the new keyword list. Then I realized I don't know how to delete records in this kind of file! Is it even possible? Does it involve some trick like opening it as a binary or stream-access type?
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Update:
In the absence of an enlightening response, I will have to do it like this:
1. Open the file
2. Skip to $COMMENTS and read all records in that portion, saving them to a scratch file.
3. Delete the file and open a new empty file with the same name, to replace the original.
4. Write the revised word list to the replacement file.
5. Write the $COMMENTS records from the scratch file to the replacement file.
6. Voila
But it seems there should be a better way? Maybe not in Fortran.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I don't see any "clever" language features that might help greatly.
Put your comments section first maybe and then overwrite the rest of the file or
1] read old file and old keywords until you get to $comments
1] create new file and write new keywords
2] continue reading old file line by line and write the lines to the new file.
3] delete the old file if that is the way or just keep versioning the file with a post fixed number, .
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I see no reason stated for keeping the two pieces of the file together. The first piece is altered in each run, the previous version being discarded. The second piece is not changed, and there is nothing stated that indicates any kind of affinity between the two pieces. So, why not keep the two parts separate?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
What I described is but a portion of the project needs. Just assume they need to stay together, otherwise future project needs get unwieldy. Similarly, the issue would be easier to code if the $KEYWORD block were last instead of first, but that's simply not where the users would expect to see it.
Thanks to the two responders for the "reality check"--I wanted to be sure I hadn't stupidly missed some regular Fortran feature.
I have solved the problem by opening a scratch file to update the original file. I first write the revised keywords to it. Then, in the original file, I move to the $COMMENTS section and copy the remaining records to the scratch file--by read followed by write, one-by-one inside a loop. Then, I delete the original file and rename the scratch file to replace it.
Quite straightforward, and relatively simple to code. Still, it seems there should be a better way. The $COMMENTS section could grow to be thousands of lines long, and the method above could become lots and lots of read-writes.
Another (similar) method I considered was to write the revised keyword block to a file RevWords.txt; write the comments block to a file Comments.txt; finally issue a system command COPY RevWords.txt+Comments.txt Newfile.txt. Maybe the system COPY operation would be faster than the Fortran read-writes, but maybe not. And, I suspect that using the system command would be less portable than the all-Fortran solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
When I posed the question "Replacing the first record in a sequential formatted file" I was thinking that maybe a way of handling your problem was to CLOSE the file and OPEN it again as a FORMATTED DIRECT access file with some bookkeeping. If you knew what byte the comments started and ended you could replace the comments byte-by-byte with DIRECT access.
Having looked more closely at your post, I realize you are attempting to replace the comments with new comments which are almost assuredly different length (number of characters). Therefore, unless you can pad one or un-pad the other so that they are ALWAYS the same length, you really have no hope of doing what I am suggesting. My suggestion would work with those web sites where they tell you "post a comment, 250 characters or less". Hence you would have a 250 character buffer in which all comments would fit.
Oh well, just a thought.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Not having the full requirements of your file on hand, one other option you have is to manipulate the file such that you overstrike the $KEYWORD with $DELETE$. IOW, don't remove the record. At some point later, you would then add a cleanup pass.
As an alternative, you could maintain a list of which records are to be ignored. The list could be placed at the end of the file. When you re-use the file, you first pick up the list. Then should you modify the file, maintain the deletes in the program, then on file close, append the deleted records list to the file.
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Could the file contents be read and saved to a character array and the changes made within the array? Would the number of rows of text or character columns be too large for memory in some cases? With the text from the file in a character array perhaps the changes to the keywords and comments and insertions could be done in memory, and then when finished just write the character array back to the file for the update. Rather than needing to shift array rows to insert new lines of text, perhaps an integer array could be used to flag the order of the lines from the character array to print to the file? The integer order array may allow new lines to be appended at the bottom of the character array, but written in the desired order for the file update.
Regards,
Greg

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page