Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
edit of a file

Subject: edit of a file

From: hamid mehdizadeh

Date: 19 Feb, 2010 22:47:04

Message: 1 of 4

Hi,
How can I edit a file, without removing the previous contents, for example if I have "father" and I want to add "e" before "a", the "a" will remove and I'll have "fether", while I need "feather". When I do my changes using fprintf or fwrite this mistake happens.
I'll be appreciated if you can help me.

Subject: edit of a file

From: Steven Lord

Date: 19 Feb, 2010 22:57:29

Message: 2 of 4


"hamid mehdizadeh" <hamidm@nt.ntnu.no> wrote in message
news:hln4d8$hm8$1@fred.mathworks.com...
> Hi,
> How can I edit a file, without removing the previous contents, for example
> if I have "father" and I want to add "e" before "a", the "a" will remove
> and I'll have "fether", while I need "feather". When I do my changes using
> fprintf or fwrite this mistake happens. I'll be appreciated if you can
> help me.

Read in the contents of the file.
Modify the contents in memory.
Write the contents to a new file.

You can _change_ or _remove_ characters in a file in-place, but you can't
_add_ characters in-place, at least not without either warping the physical
media on which the file is stored to provide space for additional bits or
copying everything after your addition some number of spaces further out
past the current end of the file and filling in your new characters.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: edit of a file

From: Sadik

Date: 19 Feb, 2010 22:58:04

Message: 3 of 4

Hi Hamid,

As far as I can tell, if you want to do that, you have to read the whole file, modify the data as you want and then write it back to the file from scratch.

Best.

"hamid mehdizadeh" <hamidm@nt.ntnu.no> wrote in message <hln4d8$hm8$1@fred.mathworks.com>...
> Hi,
> How can I edit a file, without removing the previous contents, for example if I have "father" and I want to add "e" before "a", the "a" will remove and I'll have "fether", while I need "feather". When I do my changes using fprintf or fwrite this mistake happens.
> I'll be appreciated if you can help me.

Subject: edit of a file

From: Walter Roberson

Date: 19 Feb, 2010 23:06:37

Message: 4 of 4

Steven Lord wrote:

> You can _change_ or _remove_ characters in a file in-place, but you can't
> _add_ characters in-place, at least not without either warping the physical
> media on which the file is stored to provide space for additional bits or
> copying everything after your addition some number of spaces further out
> past the current end of the file and filling in your new characters.

You cannot _remove_ characters in the file without using mex or ActiveX,
and even then only characters at the end of the file could potentially be
removed. You can write new contents to the entire existing file length, such
as what the file would contain with the characters removed, but once you reach
what you want to be the new shorter length, then you need to go outside of
Matlab to tell the operating system to truncate the file at that shorter
point. (The file truncation operation is not part of the standard C library,
but it is implemented by every operating system that Matlab is currently
implemented on.)

Adding new characters in-place was possible with the VMS filesystem using RMS
calls, but Matlab has not been supported on VMS for a number of years.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us