已知FILE指针,删除最后10个字节

我看你有戏 2014-10-29 05:18:20
已知FILE指针,删除最后10个字节
要求,不要全部导入内存
...全文
252 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-10-31
  • 打赏
  • 举报
回复
_chsize(fileno(f),_filelength(fileno(f))-10);
_chsize_s(fileno(f),_filelengthi64(fileno(f))-10i64);//超过2GB的文件
赵4老师 2014-10-31
  • 打赏
  • 举报
回复
引用 8 楼 henry3695 的回复:
[quote=引用 7 楼 zhao4zhong1 的回复:] _fileno Gets the file handle associated with a stream. int _fileno( FILE *stream ); Function Required Header Compatibility _fileno <stdio.h> Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value _fileno returns the file handle. There is no error return. The result is undefined if stream does not specify an open file. Parameter stream Pointer to FILE structure Remarks The _fileno routine returns the file handle currently associated with stream. This routine is implemented both as a function and as a macro. For details on choosing either implementation, see Choosing Between Functions and Macros. Example /* FILENO.C: This program uses _fileno to obtain * the file handle for some standard C streams. */ #include <stdio.h> void main( void ) { printf( "The file handle for stdin is %d\n", _fileno( stdin ) ); printf( "The file handle for stdout is %d\n", _fileno( stdout ) ); printf( "The file handle for stderr is %d\n", _fileno( stderr ) ); } Output The file handle for stdin is 0 The file handle for stdout is 1 The file handle for stderr is 2 Stream I/O Routines See Also _fdopen, _filelength, fopen, freopen
赵老师说中文行吗[/quote] 英语也是一门计算机语言的说。
leftbackfielder 2014-10-31
  • 打赏
  • 举报
回复

void ChangeFileLen(FILE *pFile)
{
	assert(pFile);
	fseek(pFile, 0L, SEEK_END);	
	int nFileLen = ftell(pFile);
	if (nFileLen >= 10)
	{
		int nFile = _fileno(pFile);
		if (_chsize(nFile, nFileLen-10) == 0)
		{
			printf("successfully  changed!\n");
		}
		else
		{
			printf("failed changed!");
		}
	}
	fclose(pFile);
}

mujiok2003 2014-10-30
  • 打赏
  • 举报
回复
引用 2 楼 mymtom 的回复:
linux 下可以用ftruncate
++ windows: _chsize
mujiok2003 2014-10-30
  • 打赏
  • 举报
回复
查OS的API, FILE是流, 不可能提供这样的选项。
xspace_time 2014-10-30
  • 打赏
  • 举报
回复
fseek(fp,10,SEEK_END); fputc(0xFF); fputc(0xFF); fclose(fp);
707wk 2014-10-30
  • 打赏
  • 举报
回复
引用 3 楼 poy49295 的回复:
1.先用fseek,ftell获取文件大小 2.fseek回文件头,用fread逐段内容都出来,fwrite到新的文件内容,直到写入数据量是原文件数据量少10字节
+1
我看你有戏 2014-10-30
  • 打赏
  • 举报
回复
引用 7 楼 zhao4zhong1 的回复:
_fileno Gets the file handle associated with a stream. int _fileno( FILE *stream ); Function Required Header Compatibility _fileno <stdio.h> Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value _fileno returns the file handle. There is no error return. The result is undefined if stream does not specify an open file. Parameter stream Pointer to FILE structure Remarks The _fileno routine returns the file handle currently associated with stream. This routine is implemented both as a function and as a macro. For details on choosing either implementation, see Choosing Between Functions and Macros. Example /* FILENO.C: This program uses _fileno to obtain * the file handle for some standard C streams. */ #include <stdio.h> void main( void ) { printf( "The file handle for stdin is %d\n", _fileno( stdin ) ); printf( "The file handle for stdout is %d\n", _fileno( stdout ) ); printf( "The file handle for stderr is %d\n", _fileno( stderr ) ); } Output The file handle for stdin is 0 The file handle for stdout is 1 The file handle for stderr is 2 Stream I/O Routines See Also _fdopen, _filelength, fopen, freopen
赵老师说中文行吗
勤奋的小游侠 2014-10-30
  • 打赏
  • 举报
回复
获得文件大小,然后一个字节一个字节写入新文件。写到只有10个字节时停止
poy49295 2014-10-30
  • 打赏
  • 举报
回复
1.先用fseek,ftell获取文件大小 2.fseek回文件头,用fread逐段内容都出来,fwrite到新的文件内容,直到写入数据量是原文件数据量少10字节
mymtom 2014-10-30
  • 打赏
  • 举报
回复
linux 下可以用ftruncate
赵4老师 2014-10-30
  • 打赏
  • 举报
回复
_fileno Gets the file handle associated with a stream. int _fileno( FILE *stream ); Function Required Header Compatibility _fileno <stdio.h> Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value _fileno returns the file handle. There is no error return. The result is undefined if stream does not specify an open file. Parameter stream Pointer to FILE structure Remarks The _fileno routine returns the file handle currently associated with stream. This routine is implemented both as a function and as a macro. For details on choosing either implementation, see Choosing Between Functions and Macros. Example /* FILENO.C: This program uses _fileno to obtain * the file handle for some standard C streams. */ #include <stdio.h> void main( void ) { printf( "The file handle for stdin is %d\n", _fileno( stdin ) ); printf( "The file handle for stdout is %d\n", _fileno( stdout ) ); printf( "The file handle for stderr is %d\n", _fileno( stderr ) ); } Output The file handle for stdin is 0 The file handle for stdout is 1 The file handle for stderr is 2 Stream I/O Routines See Also _fdopen, _filelength, fopen, freopen
赵4老师 2014-10-30
  • 打赏
  • 举报
回复
_chsize Changes the file size. int _chsize( int handle, long size ); Routine Required Header Optional Headers Compatibility _chsize <io.h> <errno.h> Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value _chsize returns the value 0 if the file size is successfully changed. A return value of –1 indicates an error: errno is set to EACCES if the specified file is locked against access, to EBADF if the specified file is read-only or the handle is invalid, or to ENOSPC if no space is left on the device. Parameters handle Handle referring to open file size New length of file in bytes Remarks The _chsize function extends or truncates the file associated with handle to the length specified by size. The file must be open in a mode that permits writing. Null characters ('\0') are appended if the file is extended. If the file is truncated, all data from the end of the shortened file to the original length of the file is lost. Example /* CHSIZE.C: This program uses _filelength to report the size * of a file before and after modifying it with _chsize. */ #include <io.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> void main( void ) { int fh, result; unsigned int nbytes = BUFSIZ; /* Open a file */ if( (fh = _open( "data", _O_RDWR | _O_CREAT, _S_IREAD | _S_IWRITE )) != -1 ) { printf( "File length before: %ld\n", _filelength( fh ) ); if( ( result = _chsize( fh, 329678 ) ) == 0 ) printf( "Size successfully changed\n" ); else printf( "Problem in changing the size\n" ); printf( "File length after: %ld\n", _filelength( fh ) ); _close( fh ); } } Output File length before: 0 Size successfully changed File length after: 329678 File Handling Routines See Also _close, _creat, _open
我看你有戏 2014-10-30
  • 打赏
  • 举报
回复
引用 3 楼 poy49295 的回复:
1.先用fseek,ftell获取文件大小 2.fseek回文件头,用fread逐段内容都出来,fwrite到新的文件内容,直到写入数据量是原文件数据量少10字节
引用 4 楼 lovesmiles 的回复:
获得文件大小,然后一个字节一个字节写入新文件。写到只有10个字节时停止
要求,不要全部导入内存

64,661

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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