请问有没有与Win32 API "SetEndOfFile"对应的c函数呢?

OverIsOver 2006-10-17 11:14:14
比如这样,可以快速建立一个1G的文件:
if (fp=fopen(FileName,"w")) {
fseek(fp, 1024*1024*1024, SEEK_SET);
/************************************
//这里怎样写入EOF标记呢?Win32 API "SetEndOfFile"可以实现,可我是在GNU C里编译的,用不了该函数
************************************/
fclose(fp);
}

谢谢指点一二了 :-)
...全文
362 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
westdot 2006-10-17
  • 打赏
  • 举报
回复
定位到1024*1024*1024-1的位置,写个空字符进去。
fseek(fp, (long)(1024*1024*1024-1), SEEK_SET);
fputc('\0',fp);
因为是以"w"方式打开,即使重新运行设定另一个位置也没问题。
飞哥 2006-10-17
  • 打赏
  • 举报
回复
TRUNCATE(2) Linux Programmer's Manual TRUNCATE(2)

NAME
truncate, ftruncate - truncate a file to a specified length

SYNOPSIS
#include <unistd.h>

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

DESCRIPTION
The truncate and ftruncate functions cause the regular file named by
path or referenced by fd to be truncated to a size of precisely length
bytes.

If the file previously was larger than this size, the extra data is
lost. If the file previously was shorter, it is extended, and the
extended part reads as zero bytes.

The file pointer is not changed.

With ftruncate, the file must be open for writing; with truncate, the
file must be writable.

RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is
set appropriately.

ERRORS
For truncate:

EACCES Search permission is denied for a component of the path prefix,
or the named file is not writable by the user.

EFAULT Path points outside the process's allocated address space.

EFBIG The argument length is larger than the maximum file size. (XSI)

EINTR A signal was caught during execution.

EINVAL The argument length is negative or larger than the maximum file
size.

EIO An I/O error occurred updating the inode.

EISDIR The named file is a directory.

ELOOP Too many symbolic links were encountered in translating the
pathname.

ENAMETOOLONG
A component of a pathname exceeded 255 characters, or an entire
path name exceeded 1023 characters.

ENOENT The named file does not exist.

ENOTDIR
A component of the path prefix is not a directory.

EROFS The named file resides on a read-only file system.

ETXTBSY
The file is a pure procedure (shared text) file that is being
executed.

For ftruncate the same errors apply, but instead of things that can be
wrong with path, we now have things that can be wrong with fd:

EBADF The fd is not a valid descriptor.

EBADF or EINVAL
The fd is not open for writing.

EINVAL The fd does not reference a regular file.

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧