在 Matlab 中写入文件时从 EOF 中删除字符

发布于 2024-08-10 12:48:54 字数 399 浏览 1 评论 0原文

在 Matlab 中,创建一定数量的行并将其打印到文件中后,我需要删除一行并将其余数据重写到同一文件中。当我这样做时,新数据会覆盖以前的数据,但由于数据比原始数据短,因此仍然有原始数据的残余。有谁知道删除额外数据的最佳/最有效方法是什么?

这是我想要做的一个简化的例子:

fid = fopen('file.txt','w');  
for i=1:10  
    fprintf(fid,'%i\r\t',i);  
end  
frewind(fid);  
for i=3:5  
    fprintf(fid,'%i\r\t',i);  
end  
fprintf(fid,'EOF');  
fclose(fid);  

我已经查遍了,但我似乎找不到我的问题的解决方案。有什么建议吗?

In Matlab, after creating a certain number of lines and printing them to a file, I have the need to delete a line and rewrite the rest of the data to that same file. When I do so, the new data overwrites the previous data, but since the data is shorter than the original, there are still remnants of the original data. Does anyone have any idea what the best/most efficient way to delete that extra data is?

Here is a simplified example of what I'm trying to do:

fid = fopen('file.txt','w');  
for i=1:10  
    fprintf(fid,'%i\r\t',i);  
end  
frewind(fid);  
for i=3:5  
    fprintf(fid,'%i\r\t',i);  
end  
fprintf(fid,'EOF');  
fclose(fid);  

I've looked all over, but I can't seem to find the solution to my question. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妳是的陽光 2024-08-17 12:48:54

在不使用任何临时文件的情况下,您可以执行以下操作:

fid = fopen('file.txt', 'wt');
for i=1:10
    fprintf(fid, '%i\n', i);
end
frewind(fid);  
for i=3:5
    fprintf(fid, '%i\n', i);
end
pos = ftell(fid);             % get current position in file
fclose(fid);

% read from begining to pos
fid = fopen('file.txt', 'r');
data = fread(fid, pos);
fclose(fid);

% overwite file with data read
fid = fopen('file.txt', 'w');
fwrite(fid, data);
fclose(fid);

Without using any temp files, you can do the following:

fid = fopen('file.txt', 'wt');
for i=1:10
    fprintf(fid, '%i\n', i);
end
frewind(fid);  
for i=3:5
    fprintf(fid, '%i\n', i);
end
pos = ftell(fid);             % get current position in file
fclose(fid);

% read from begining to pos
fid = fopen('file.txt', 'r');
data = fread(fid, pos);
fclose(fid);

% overwite file with data read
fid = fopen('file.txt', 'w');
fwrite(fid, data);
fclose(fid);
傲鸠 2024-08-17 12:48:54

打印“EOF”不起作用 - 不错的尝试!

Unix 系统调用 truncateftruncate 可以做到这一点,只要在第一个参数中给出文件描述符 (truncate) 或句柄 (ftruncate) 以及在第二。

我会尝试看看 Matlab 是否支持 ftruncate。如果失败的话......如果最坏的情况发生,您可以将文件复制写入到新文件,当您到达您认为的数据末尾时停止并关闭新文件。

Printing "EOF" won't work - nice try!

There are Unix system calls truncate and ftruncate that will do that, given either a file descriptor (truncate) or handle (ftruncate) in the first argument and a desired length in the second.

I'd try and see if Matlab supports ftruncate. Failing that... if worst came to worst you could copy-write the file to a new file, stopping and closing the new file when you hit what you consider the end of data.

£噩梦荏苒 2024-08-17 12:48:54

跟进 Carl Smotricz 的 建议使用两个文件,可以使用 MATLAB 的 DELETE< /a> 和 MOVEFILE 命令以避免系统调用:

fid = fopen('file.txt','wt');
for i=1:10
    fprintf(fid,'\t%i\r',i);
end
fclose(fid);

fid = fopen('file.txt','rt');
fidNew = fopen('fileNew.txt', 'wt');
for i = 1:2
    s = fgetl(fid);
    fprintf(fidNew, '%s\r', s);
end
for i=4:10
    fprintf(fidNew, '\t%i\r', i);  
end
fclose(fid);
fclose(fidNew);

delete('file.txt');
movefile('fileNew.txt', 'file.txt')

To follow up on Carl Smotricz's suggestion of using two files, you can use MATLAB's DELETE and MOVEFILE commands to avoid system calls:

fid = fopen('file.txt','wt');
for i=1:10
    fprintf(fid,'\t%i\r',i);
end
fclose(fid);

fid = fopen('file.txt','rt');
fidNew = fopen('fileNew.txt', 'wt');
for i = 1:2
    s = fgetl(fid);
    fprintf(fidNew, '%s\r', s);
end
for i=4:10
    fprintf(fidNew, '\t%i\r', i);  
end
fclose(fid);
fclose(fidNew);

delete('file.txt');
movefile('fileNew.txt', 'file.txt')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文