- read the file into memory and operate on it in memory to make the changes desired, then rewrite the modified file in its entirety, or
- as you're doing read a record at a time, make any modifications needed, then write the original/modified record to a new file.
Append and add lines in text/dat file
18 次查看(过去 30 天)
显示 更早的评论
I have a .dat file from CFD postprocessing with variable data regularly listed. I am currently reading through the files fine, but I want to add variable data. I attached an image of the data structure below (this is only an excerpt; the full file is hundreds of thousands of lines, which is the reason that I can't do it manually).
Right now, I am just reading the data. But I will need to add a few things, among them:
- Two new lines after line 14, but before line 15.
- Two numbers apppended to the end of each variable line; for example, adding " 0.000000000E+00 0.000000000E+00" to the end of line 21 (the numbers themselves will be calculated using a routine I've already completed)
- Ignoring lines with two numbers on them.
I have the logic worked out, but I am having trouble a) figuring out how to overwrite the current line/append data to a line itself, and b) formatting numbers the same way: scientific notation with 9 digits (easy), captial E (easy), and a plus/minus with two numbers for the exponent.
I have been using fgetl to read through the data, and the easiest way to do this would be (if appending itself is impossible) to rewrite the line in its entirety by simply setting the current line to a different string. There have been a few posts that I've seen that say this is impossible, but I find this hard to believe. Writing data algorithmically like this seems too easy not to be possible.
Thanks for any help.
0 个评论
采纳的回答
dpb
2022-7-29
编辑:dpb
2022-7-29
"..., and the easiest way to do this would be (if appending itself is impossible) to rewrite the line in its entirety by simply setting the current line to a different string. There have been a few posts that I've seen that say this is impossible, but I find this hard to believe."
Well, believe it in general.
Sequential text files are, well, "sequential" -- they're just a string of bytes -- the only way to replace text in line in a text file (that isn't record oriented as is supported by Fortran, but NOT supported by C/MATLAB) is if the new line is exactly the same number of characters as the line it is replacing. Otherwise it will overwirte subsequent characters if longer or leave old data that was part of the previous line if write fewer characters.
The way to do something like this is either
With large files that may be too large for memory, the second is the obvious choice...I've written and posted several such filters on Answers in the past; unfortunately I don't have a link to any of them to point to a particular instance.
8 个评论
dpb
2022-7-31
It is simple -- in principle. The problem is in the details and that sequential files are, well, "sequential". Remember they're just a stream of bytes, there's nothing in the file or the OS that discerns any one byte from any other; it's all up to the application code to do with those bytes what it wishes.
Besides the particular languages that have such facilities incorporated into them, there are other languages like Fortran or OS'es that support files with record markers such that you can rewrite records -- but those don't include C and it is the C i/o library that MATLAB uses underneath and TMW has not chosen to date to incorporate them into the language.
It and formatted i/o are two areas from Fortran would have been nice to have brought over from its original FORTRAN heritage.
Walter Roberson
2022-7-31
MATLAB used to be supported on DEC VAX VMS, which is an operating system that really did support changing lines in the middle of text files. Text files were effectively databases with a sequence number for each record, so you could rewrite a line by changing the record associated with that sequence number, and you could insert lines by using an unused sequence number between the existing records.
However, VMS supported two kinds of files, one as described, and one which was just a stream of bytes. And MATLAB never supported operations on the one described above (it required calls to a different I/O library that was specific to VMS and the older DEC RSX operating system.)
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!