Re: Need advice on File I/O



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
.



Relevant Pages

  • Re: Help is needed to compile C program using Visual Studie 2005
    ... the pdb file that was used when this precompiled header was created, ... an output file whose name has the following format: ... The length of input file paths and name must be less than 256; ... while(i < DefinedVariableArrayIndex) { ...
    (microsoft.public.vc.language)
  • Re: Help with pattern matching
    ... then print the line to an output file. ... > replica of the input file. ... In Perl, CamelBack is generall reserved for package names. ... where their meaning must be expressed in comments. ...
    (perl.beginners)
  • Re: Help with pattern matching
    ... then print the line to an output file. ... > replica of the input file. ... If you had had warnings enabled as well as strict you might have found ... > # Assign the second field to an evaluation scalar ...
    (perl.beginners)
  • Re: Difficult text file to parse.
    ... > records are which there are only two, look at the output file below to ... I want to show the delimiters even if ... > My sample Input file: ... [sample input and output files with long fields snipped] ...
    (comp.lang.perl.misc)