MATLAB - 删除二进制文件的元素而不加载整个文件

发布于 2024-08-27 22:49:05 字数 233 浏览 8 评论 0原文

这可能是一个愚蠢的问题,但 Google 和 MATLAB 文档让我失望了。我有一个相当大的二进制文件(> 10 GB),我需要打开并删除最后四千万字节左右。有没有办法做到这一点,而无需将整个文件分块读取到内存中并将其打印到新文件中?生成该文件花了 6 个小时,所以我一想到要重新阅读整个内容就感到畏缩。

编辑:

该文件的大小为 14,440,000,000 字节。我需要把它削减到 14,400,000,000。

This may be a stupid question, but Google and MATLAB documentation have failed me. I have a rather large binary file (>10 GB) that I need to open and delete the last forty million bytes or so. Is there a way to do this without reading the entire file to memory in chunks and printing it out to a new file? It took 6 hours to generate the file, so I'm cringing at the thought of re-reading the whole thing.

EDIT:

The file is 14,440,000,000 bytes in size. I need to chop it to 14,400,000,000.

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

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

发布评论

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

评论(4

韶华倾负 2024-09-03 22:49:05

Matlab 中没有 ftruncate(),但您可以访问 Matlab 中嵌入的 JVM 中的完整 Java 标准库,并且可以使用 java.io.RandomAccessFile 或 Java NIO 类来截断文件。

下面是一个 Matlab 函数,它调用 Java 来删除文件的最后 n 个字节。应具有最小的 I/O 成本。

function remove_last_n_bytes_from_file(file, n)

jFile = java.io.RandomAccessFile(file, 'rw');
currentLength = jFile.length();
wantLength = currentLength - n;
fprintf('Truncating file %s: Resizing to %d to remove %d bytes\n', file, wantLength, n);
jFile.setLength(wantLength);
jFile.close();

你也可以把它当作一句台词来做。

java.io.RandomAccessFile('/path/to/my/file.bin', 'rw').setLength(n);

There is no ftruncate() in Matlab, but you've got access to the full Java standard library in the JVM embedded in Matlab, and can use java.io.RandomAccessFile or the Java NIO classes to truncate a file.

Here's a Matlab function that calls to Java to lop the last n bytes off a file. Should have minimal I/O cost.

function remove_last_n_bytes_from_file(file, n)

jFile = java.io.RandomAccessFile(file, 'rw');
currentLength = jFile.length();
wantLength = currentLength - n;
fprintf('Truncating file %s: Resizing to %d to remove %d bytes\n', file, wantLength, n);
jFile.setLength(wantLength);
jFile.close();

You could also do it as a one-liner.

java.io.RandomAccessFile('/path/to/my/file.bin', 'rw').setLength(n);
彼岸花似海 2024-09-03 22:49:05

我发现 Perl 比 MATLAB 执行此操作要快得多。

以下是 Perl Cookbook 中的两个示例:

truncate(HANDLE, $length)
    or die "Couldn't truncate: $!\n";

truncate("/tmp/$.pid", $length)
    or die "Couldn't truncate: $!\n";

您可以使用以下命令从 MATLAB 运行 Perl 脚本 : PERL 函数。

I found Perl is much quicker to do this than MATLAB.

Here are two examples from Perl Cookbook:

truncate(HANDLE, $length)
    or die "Couldn't truncate: $!\n";

truncate("/tmp/$.pid", $length)
    or die "Couldn't truncate: $!\n";

You can run Perl script from MATLAB with PERL function.

初懵 2024-09-03 22:49:05

由于您不想将文件读入 MATLAB(可以理解),因此您正在处理系统级命令。 MATLAB 具有使用“system”命令调用系统命令的功能

system

因此,现在您的问题已简化为在操作系统中找到可以为您完成此操作的 shell 命令。或者您可以使用 truncate() (unix -- KennyTM) 或 SetEndOfFile (windows) 编写程序

Since you don't want to read the file into MATLAB (understandably), you are dealing with system level commands. MATLAB has a facility to call system commands using the "system" command

system

So now your problem is reduced to finding the shell command in your OS that will do it for you. Or you can write a program using truncate() (unix -- KennyTM) or SetEndOfFile (windows)

忆沫 2024-09-03 22:49:05

我不知道 MATLAB 是否支持此功能,但请参阅 ftruncate()truncate()

I don't know if MATLAB supports this, but see ftruncate() and truncate().

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