windows 截断文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/584639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Truncate file
提问by Adam Davis
I am using fopen fseeko64 ftello64 fclose etc. to operating on a file.
我正在使用 fopen fseeko64 ftello64 fclose 等对文件进行操作。
How can I truncate a file? I know that there is no standard way to do this in C. All I want is some way that will work on any win32 platform. I'm using mingw gcc to compile.
如何截断文件?我知道在 C 中没有标准的方法可以做到这一点。我想要的只是一种可以在任何 win32 平台上工作的方法。我正在使用 mingw gcc 进行编译。
Please note: I meant truncate the size of the file to a specified size, not make it 0 size. And using a trick like copy part to another file and delete/rename is not appropriate.
请注意:我的意思是将文件的大小截断为指定的大小,而不是将其设为 0 大小。并且使用像将部分复制到另一个文件和删除/重命名这样的技巧是不合适的。
回答by Adam Davis
Get a handle to the file with write access, set the file pointer, then call SetEndOfFile().
获取具有写访问权限的文件句柄,设置文件指针,然后调用 SetEndOfFile()。
-Adam
-亚当
回答by paxdiablo
If you want to truncate the file to zero size, you can fopen
with the "w"
flag:
如果要将文件截断为零大小,可以fopen
使用以下"w"
标志:
FILE *fh = fopen("file.txt","w");
if (fh != NULL) fclose(fh);
For truncating to a specific size in standard C, you can do this with a transfer/rename solution, something like:
为了在标准 C 中截断到特定大小,您可以使用传输/重命名解决方案来执行此操作,例如:
FILE *finp = fopen ("inp.txt", "rb"); // should check for NULLs
FILE *fout = fopen ("out.txt", "wb");
size_t sz = 100000; // 100,000 bytes
char *buff = malloc (sz); // should check for NULL
sz = fread (buff, 1, sz, fin); // should check for errors
fwrite (buff, 1, sz, fout);
free (buff);
fclose (fin);
fclose (fout);
rename ("out.txt", "inp.txt); // should check for error
Of course, if you have access to the Win32 headers and libraries (and I believe MinGW gives you this), you can use SetEndOfFile()
, since it does it in place, rather than having to create a new file and then rename it.
当然,如果您可以访问 Win32 头文件和库(我相信 MinGW 为您提供了这个),您可以使用SetEndOfFile()
,因为它就地完成了,而不必创建一个新文件然后重命名它。
That means using Windows handle-based file I/O rather than the C FILE*
-based but, if you're limiting yourself to Windows anyway, that may not matter. If you want portability on the other hand, you'll need a solution based on standard C, such as the transfer/rename solution above.
这意味着使用基于 Windows 句柄的文件 I/O 而不是FILE*
基于C的文件 I/O,但是,如果您无论如何都将自己限制在 Windows,那可能无关紧要。另一方面,如果您想要可移植性,您将需要一个基于标准 C 的解决方案,例如上面的传输/重命名解决方案。
回答by Martyn Davis
For FILE based file operations, use _fileno() and _chsize_s() to change the size of a file.
对于基于 FILE 的文件操作,使用 _fileno() 和 _chsize_s() 来更改文件的大小。
int changesize(FILE *fp, __int64 size)
{
int filedes = _fileno(fp);
return _chsize_s(filedes, size);
}
A truncate version can be written by validating that the supplied size is less than the current file size, as _chsize_s() will truncate or extend a file's size - see http://msdn.microsoft.com/en-us/library/whx354w1(VS.80).aspx.
可以通过验证提供的大小是否小于当前文件大小来编写截断版本,因为 _chsize_s() 将截断或扩展文件的大小 - 请参阅http://msdn.microsoft.com/en-us/library/whx354w1 (VS.80).aspx。
回答by Thomas
If you simply fopen()
a file with the "w"
argument, it will be truncated.
如果你只是fopen()
一个带有"w"
参数的文件,它将被截断。
http://www.cplusplus.com/reference/clibrary/cstdio/fopen.html
http://www.cplusplus.com/reference/clibrary/cstdio/fopen.html
回答by DeadHead
As mentioned already, you can use fopen() with the "w" flag like:
如前所述,您可以将 fopen() 与“w”标志一起使用,例如:
FILE *f = fopen("file.txt", "w");
Also, if you already have the file opened, you can use the function freopen(), again with the "w" flag:
此外,如果您已经打开了文件,您可以使用函数 freopen(),再次使用“w”标志:
FILE *f = fopen("file.txt", "r"); //initial fopen() call
...
f = freopen("file.txt", "w", f); //reopens "file.txt" and truncates it
http://www.cplusplus.com/reference/clibrary/cstdio/freopen.html
http://www.cplusplus.com/reference/clibrary/cstdio/freopen.html
EDIT: After seeing you've edited your OP, I won't repost what Pax and Adam Davis has already put. Also, I'll confirm what Pax said, that the MinGW does give you access to the Win32 headers.
编辑:在看到你编辑了你的 OP 之后,我不会重新发布 Pax 和 Adam Davis 已经发表的内容。另外,我将确认 Pax 所说的话,MinGW 确实可以让您访问 Win32 标头。