Matlab txt文件 替换内容 fgetl fseek cof

Data Import and Export  :Low-Level File I/O

the contents of the file:
   16     5     9     4
    2    11     7    14
    3    10     6    15
   13     8    12     1
   55    55    55    55
Example — Overwriting an Existing Text File.  Replace the third line of the file changing.txt from the previous example with [33 33 33 33]:
将第三行换成33 33 33 33

replaceLine = 3;
myformat = '%5d %5d %5d %5d\n';
% Open the file with permission to read and update.
% Use fgetl, which reads a line at a time,
% to place the file position indicator at the third line.
fid = fopen('changing.txt','r+');
for k=1:(replaceLine-1);
    fgetl(fid);
end;
%将指针移至第三行起始处
% call fseek between read and write operations
fseek(fid, 0, 'cof');
% 在当前位置开始写入
% print the new values
fprintf(fid, myformat, [33 33 33 33]);
%将第三行替换
% close the file
fclose(fid);

To view the file, use the type function:
type changing.txt
This command returns the new contents of the file:
   16     5     9     4
    2    11     7    14
   33    33    33    33
   13     8    12     1
   55    55    55    55

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值