Re: Need advice on File I/O
- From: roberson@xxxxxxxxxxxxxxxxxx (Walter Roberson)
- Date: Sat, 1 Sep 2007 02:02:18 +0000 (UTC)
In article <fbaa6i$3pu$1@xxxxxxxxxxxxxxxxxx>, G.A.M. <x0zero@xxxxxxxxx> wrote:
I am working with delimited text files (mostly, but not
always, comma separated or CSV).
Reading and creating entire files isn't a problem. My
problems start when I need to alter data in existing files.
I could use some advice.
Sometimes I will need to remove an entire line from a file.
Sometimes I need to replace a single value (at any row,
column location) without changing the rest of the file.
You only really have three choices:
1) open the input file and open an output file, copy from input to
output until you find what you need to change, write the changed
data to the output file, then write the rest of the input to the
output. This will work provided there is enough disk space for
the second copy of the file, and is much safer than the other
options because if something goes wrong in the middle of the
process, you would still have the input file unchanged.
2) open the file 'r+', find the location you need to change, and
rewrite the entire rest of the file from that point. If the new data
turns out to be shorter than the original data, there is no way
to say "truncate the file here", so you would have to blank-pad
or put in a comment or otherwise put in something that the
next program along will ignore.
3) as in (2), but if the modified line turns out to be shorter
than the original line, blank pad or otherwise insert some kind
of comment that the next program along will ignore, until you
have filled up the empty space (remember to leave room for
the end of line character(s)). If, though, the modified line
turns out to be longer than the original line, the rest of the
file will have to be rewritten (but in that situation, you won't
need any padding at the end of the file.)
There is no mechanism in Matlab's file I/O to insert or delete
characters (or bytes) in a text or binary file. That's because
none of the underlying file systems offer support for such an
operation -- no way to do it in Windows, no way to do it in Unix.
On all currently supported operating systems, files are just
streams of characters. Think of it like being a piece of paper:
you can white-out and write over any particular place, but cutting
the paper to remove a section or add a new section isn't allowed
(nor is writing smaller, nor is writing in the margin.)
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
.
- References:
- Need advice on File I/O
- From: G.A.M.
- Need advice on File I/O
- Prev by Date: Convert a decimal to base 64 system
- Next by Date: Re: V 7.4.0 on Core2 Duo vs. V 7.1.0 on PentM
- Previous by thread: Need advice on File I/O
- Next by thread: Re: Need advice on File I/O
- Index(es):
Relevant Pages
|