C语言中如何截断文件?

发布于 2024-07-20 18:21:39 字数 87 浏览 12 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(6

八巷 2024-07-27 18:21:39

如果您想将文件的先前内容保留到一定长度(长度大于零,其他答案提供),则 POSIX 提供 truncate()ftruncate() 作业函数。

#include <unistd.h>
int ftruncate(int fildes, off_t length);
int truncate(const char *path, off_t length);

该名称表明了主要目的 - 缩短文件。 但是,如果指定的长度比先前的长度长,则文件将增长(零填充)到新的大小。 请注意,ftruncate() 适用于文件描述符,而不是FILE *; 你可以使用:

if (ftruncate(fileno(fp), new_length) != 0) ...error handling...

但是,你应该意识到,混合文件流(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() and ftruncate() functions for the job.

#include <unistd.h>
int ftruncate(int fildes, off_t length);
int truncate(const char *path, off_t length);

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 a FILE *; you could use:

if (ftruncate(fileno(fp), new_length) != 0) ...error handling...

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 function SetFileValidData() 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.

如梦亦如幻 2024-07-27 18:21:39

在 Windows 系统中,没有标头 但您可以使用以下方法截断文件

FILE *f = ...;
_chsize( fileno(f), size);

In Windows systems there's no header <unistd.h> but yet you can truncate a file by using

FILE *f = ...;
_chsize( fileno(f), size);
瞎闹 2024-07-27 18:21:39

这是操作系统的功能。 标准 POSIX 方法是:

open("file", O_TRUNC | O_WRONLY);

That's a function of your operating system. The standard POSIX way to do it is:

open("file", O_TRUNC | O_WRONLY);
寒江雪… 2024-07-27 18:21:39

如果要在某种风格的 UNIX 下运行,这些 API 应该可用:

   #include <unistd.h>
   #include <sys/types.h>

   int truncate(const char *path, off_t length);
   int ftruncate(int fd, off_t length);

根据我的 Linux 机器上的“man truncate”,这些 API 符合 POSIX 标准。 请注意,如果您传递的长度大于当前长度,这些调用实际上会增加文件的大小(!)。

If this is to run under some flavor of UNIX, these APIs should be available:

   #include <unistd.h>
   #include <sys/types.h>

   int truncate(const char *path, off_t length);
   int ftruncate(int fd, off_t length);

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.

远昼 2024-07-27 18:21:39

啊,你编辑了你的帖子,你正在使用C。当你打开文件时,用“w+”模式打开它,就像这样,它会截断它以准备写入:

FILE* f = fopen("C:\\gabehabe.txt", "w+");
fclose(file);

要在 C++ 中截断文件,您只需为该文件创建一个 ofstream 对象,使用 ios_base::trunc 作为文件模式来截断它,像这样:

ofstream x("C:\\gabehabe.txt", 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:

FILE* f = fopen("C:\\gabehabe.txt", "w+");
fclose(file);

</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:

ofstream x("C:\\gabehabe.txt", ios_base::trunc);
不知在何时 2024-07-27 18:21:39

如果您想截断整个文件,打开文件进行写入即可完成此操作。 否则,您必须打开文件进行读取,并将文件中要保留的部分读取到临时变量中,然后将其输出到您需要的任何地方。

截断整个文件:

FILE *file = fopen("filename.txt", "w"); //automatically clears the entire file for you.

截断文件的一部分:

FILE *inFile = fopen("filename.txt", "r");
//read in the data you want to keep
fclose(inFile);
FILE *outFile = fopen("filename.txt", "w");
//output back the data you want to keep into the file, or what you want to output.
fclose(outFile);

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:

FILE *file = fopen("filename.txt", "w"); //automatically clears the entire file for you.

Truncate part of the file:

FILE *inFile = fopen("filename.txt", "r");
//read in the data you want to keep
fclose(inFile);
FILE *outFile = fopen("filename.txt", "w");
//output back the data you want to keep into the file, or what you want to output.
fclose(outFile);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文