Home > Archive > Matlab > August 2005 > File writing Problem
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
File writing Problem
|
|
|
| Hi all,
I am writing some data to the text file. When I execute the program
the content is written in the text file when I execute the same
program second time it's appending to the file I want the previous
content to be flushed and new data to be written in the text file.
I have tried using a and a+ options. But it's not woking out. Is
there a way I can do it?.
Thanks in advance
Vamsi
| |
| Jérôme 2005-08-31, 7:01 pm |
| Hi,
you can read in the documentation :
'r' Open file for reading (default).
'w' Open file, or create new file, for writing; discard existing
contents, if any.
'a' Open file, or create new file, for writing; append data to the
end of the file.
'r+' Open file for reading and writing.
'w+' Open file, or create new file, for reading and writing; discard
existing contents, if any.
'a+' Open file, or create new file, for reading and writing; append
data to the end of the file.
'A' Append without automatic flushing; used with tape drives.
'W' Write without automatic flushing; used with tape drives.
Jérôme
| |
|
| Hi,
I have tried using all the options. I just want to know if there is a
option where I can append the data to the file and discard exisitng
contents if any. This discarding should happen when the program is
executed the second time.
Jérôme wrote:
>
>
> Hi,
>
> you can read in the documentation :
>
> 'r' Open file for reading (default).
> 'w' Open file, or create new file, for writing; discard existing
> contents, if any.
> 'a' Open file, or create new file, for writing; append data to the
> end of the file.
> 'r+' Open file for reading and writing.
> 'w+' Open file, or create new file, for reading and writing;
> discard
> existing contents, if any.
> 'a+' Open file, or create new file, for reading and writing; append
> data to the end of the file.
> 'A' Append without automatic flushing; used with tape drives.
> 'W' Write without automatic flushing; used with tape drives.
>
> Jérôme
| |
| Jérôme 2005-08-31, 7:01 pm |
| What's about this one :
'w+' Open file, or create new file, for reading and writing; discard
existing contents, if any.
Jérôme
| |
|
| I have 4 operations which are to done on two numbers let's say
addition,sbtraction, division and multiplication.The output resulted
due to these operation should be stored in a text file one after the
other. When I execute the program first time it's writing the output
onto the file. When I reexecute the program once again it's appending
to the file but not discarding the previous content.This happens when
I use a+ option. When i use the w+ option it's discarding the
previous content but it's writng only the fourth operation and
leaving the rest three.
I am struck and I don't know how to proceed.
Thxs
Vamsi
Jérôme wrote:
>
>
> What's about this one :
>
> 'w+' Open file, or create new file, for reading and writing;
> discard
> existing contents, if any.
>
> Jérôme
| |
| Jérôme 2005-08-31, 7:01 pm |
| Is it a homework or something ?
a=15;
b=4;
fid=fopen('results.txt','wt');
fprintf(fid,'%d\n',a+b);
fprintf(fid,'%d\n',a-b);
fprintf(fid,'%.2f\n',a/b);
fprintf(fid,'%d\n',a*b);
fclose(fid);
Jérôme
| |
|
| It's not a homework or something. I have just taken one example to
explain you the problem I am facing. The thing is that if i want to
perform the same operations on 5 different pairs of numbers and
append them to the file how can I do it?
Jérôme wrote:
>
>
> Is it a homework or something ?
>
> a=15;
> b=4;
>
> fid=fopen('results.txt','wt');
> fprintf(fid,'%d\n',a+b);
> fprintf(fid,'%d\n',a-b);
> fprintf(fid,'%.2f\n',a/b);
> fprintf(fid,'%d\n',a*b);
> fclose(fid);
>
> Jérôme
| |
|
| Hi,
use fopen only once! and use the fid as many times as you wish!
this along with w+ should solve your problem.
regards
gopla
| |
| Peter Boettcher 2005-08-31, 7:01 pm |
| Vamsi <vamsi_219@yahoo.com> writes:
> It's not a homework or something. I have just taken one example to
> explain you the problem I am facing. The thing is that if i want to
> perform the same operations on 5 different pairs of numbers and
> append them to the file how can I do it?
Open the file with 'a' if you want to add to the end of it. Open it
with 'w' if you want to overwrite the old contents. Only you can
decide according to your application when to use which one.
Remember you only have to open the file once per program. So open
with 'w' and fwrite or fprintf all the results you want, then fclose
at the end. Does that help?
--
Peter Boettcher <boettcher@ll.mit.edu>
MIT Lincoln Laboratory
MATLAB FAQ: http://www.mit.edu/~pwb/cssm/
| |
| Jérôme 2005-08-31, 7:01 pm |
| I think you should start by reading this :
<http://www.mathworks.com/access/hel...g/ch_imp50.html>
Try this (I know that it's a simple example) :
a=[1 2 2 5 4];
b=[3 3 4 1 1];
n=length(a);
fid=fopen('results.txt','wt');
fprintf(fid,[repmat('%d ',1,n) '\n'],a+b);
fprintf(fid,[repmat('%d ',1,n) '\n'],a-b);
fprintf(fid,[repmat('%.2f ',1,n) '\n'],a./b);
fprintf(fid,[repmat('%d ',1,n) '\n'],a.*b);
fclose(fid);
Jérôme
|
|
|
|
|