フィルターのクリア

Change/modify data in file?

10 ビュー (過去 30 日間)
Nora
Nora 2013 年 10 月 27 日
コメント済み: Nora 2013 年 10 月 28 日
How would I write a script that would replace all occurrences of the old name with the new name and saving this as a new file. To replace the string, I used strrep(), but I am not sure how to use it and to also to save changes onto a new file. What is wrong with my script?
Data file looks as so:
Vendor Customer
Acme XYZ
Tulip2you Flowers4me
Flowers4me Acme
XYZ Cartesian
My script is:
data1 = 'vendorcust.dat';
FID = fopen(data1,'w');
file = type(data1);
newdata = strrep(file, 'Acme','NewAcme');
  1 件のコメント
Jan
Jan 2013 年 10 月 27 日
編集済み: Jan 2013 年 10 月 27 日
Please explain, why you think, that something is wrong with the script. Do not let us guess, what you found out already.

サインインしてコメントする。

採用された回答

Jan
Jan 2013 年 10 月 27 日
type is not thought for importing the contents of a file, but it works. I do not see any trial to create a new file.
FileName = 'vendorcust.dat'; % "data1" is a bad name for the name of a file
Data = fileread(FileName); % Import the file's contents
Data = strrep(Data, 'Acme', 'NewAcme'); % Apply the modification
FID = fopen('vendorcust_new.dat'); % Create a new file
if FID == -1, error('Cannot open file'); end % Always check for success
fwrite(FID, Data, 'char'); % Write data
fclose(FID); % Closing is important
This is a little bit too much help for a homework question. Please consider, that submitting it without mentioning the sources is cheating. But I'm tired and it is harder to assist than to solve here. Sorry!
  1 件のコメント
Nora
Nora 2013 年 10 月 28 日
This does make sense. Thank you for taking the time to explain each part!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLow-Level File I/O についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by