C语言中如何截断文件?
我正在使用 C 将一些数据写入文件。 我想删除文件中以前写入的文本,以防它比我现在写的内容长。 我想减小文件的大小或截断直到最后。 我怎样才能做到这一点?
I'm using C to write some data to a file. I want to erase the previous text written in the file in case it was longer than what I'm writing now.
I want to decrease the size of file or truncate until the end. How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您想将文件的先前内容保留到一定长度(长度大于零,其他答案提供),则 POSIX 提供
truncate()
和ftruncate()
作业函数。该名称表明了主要目的 - 缩短文件。 但是,如果指定的长度比先前的长度长,则文件将增长(零填充)到新的大小。 请注意,
ftruncate()
适用于文件描述符,而不是FILE *
; 你可以使用:但是,你应该意识到,混合文件流(
FILE *
)和文件描述符(int
)访问单个文件很容易导致混乱 -请参阅一些问题的评论。 这应该是最后的手段。不过,出于您的目的,打开时截断可能就是您所需要的,为此,其他人提供的选项就足够了。
对于 Windows,有一个函数 < code>SetEndOfFile() 和相关函数
SetFileValidData()
函数可以完成类似的工作,但使用不同的界面。 基本上,您寻找要设置文件结尾的位置,然后调用该函数。还有一个函数
_chsize()
,如 answer 中所述://stackoverflow.com/users/103521/sofr">sofr。If you want to preserve the previous contents of the file up to some length (a length bigger than zero, which other answers provide), then POSIX provides the
truncate()
andftruncate()
functions for the job.The name indicates the primary purpose - shortening a file. But if the specified length is longer than the previous length, the file grows (zero padding) to the new size. Note that
ftruncate()
works on a file descriptor, not aFILE *
; you could use:However, you should be aware that mixing file stream (
FILE *
) and file descriptor (int
) access to a single file is apt to lead to confusion — see the comments for some of the issues. This should be a last resort.It is likely, though, that for your purposes, truncate on open is all you need, and for that, the options given by others will be sufficient.
For Windows, there is a function
SetEndOfFile()
and a related functionSetFileValidData()
function that can do a similar job, but using a different interface. Basically, you seek to where you want to set the end of file and then call the function.There's also a function
_chsize()
as documented in the answer by sofr.在 Windows 系统中,没有标头
但您可以使用以下方法截断文件In Windows systems there's no header
<unistd.h>
but yet you can truncate a file by using这是操作系统的功能。 标准 POSIX 方法是:
That's a function of your operating system. The standard POSIX way to do it is:
如果要在某种风格的 UNIX 下运行,这些 API 应该可用:
根据我的 Linux 机器上的“man truncate”,这些 API 符合 POSIX 标准。 请注意,如果您传递的长度大于当前长度,这些调用实际上会增加文件的大小(!)。
If this is to run under some flavor of UNIX, these APIs should be available:
According to the "man truncate" on my Linux box, these are POSIX-conforming. Note that these calls will actually increase the size of the file (!) if you pass a length greater than the current length.
啊,你编辑了你的帖子,你正在使用C。当你打开文件时,用“w+”模式打开它,就像这样,它会截断它以准备写入:
要在 C++ 中截断文件,您只需为该文件创建一个 ofstream 对象,使用 ios_base::trunc 作为文件模式来截断它,像这样:
<edit>
Ah, you edited your post, you're using C. When you open the file, open it with the mode "w+" like so, and it will truncate it ready for writing:
</edit>
To truncate a file in C++, you can simply create an ofstream object to the file, using ios_base::trunc as the file mode to truncate it, like so:
如果您想截断整个文件,打开文件进行写入即可完成此操作。 否则,您必须打开文件进行读取,并将文件中要保留的部分读取到临时变量中,然后将其输出到您需要的任何地方。
截断整个文件:
截断文件的一部分:
If you want to truncate the entire file, opening the file up for writing does that for you. Otherwise, you have to open the file for reading, and read the parts of the file you want to keep into a temporary variable, and then output it to wherever you need to.
Truncate entire file:
Truncate part of the file: