C++中fprintf(fp,"")产生的文件,马上操作,发现fp文件只有一部分,怎么办

yanpeace 2015-04-21 03:54:36
C++code

while((ptr=fgets(buf,1024,fp) != NULL){
if( strstr(ptr , "comment") != NULL){
continue;
}
else {
fprintf(fp,"%s", ptr);
}
}

产生的文件应该是10000行,但是马上对fp文件操作,发现fp文件只有9000行,怎么解决,谢谢!
...全文
275 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yanpeace 2015-04-22
  • 打赏
  • 举报
回复
引用 3 楼 zhao4zhong1 的回复:
fflush(fp1);
非常感谢您~
赵4老师 2015-04-21
  • 打赏
  • 举报
回复
fflush(fp1);
yanpeace 2015-04-21
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
fflush Flushes a stream. int fflush( FILE *stream ); Function Required Header Compatibility fflush <stdio.h> ANSI, 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 fflush returns 0 if the buffer was successfully flushed. The value 0 is also returned in cases in which the specified stream has no buffer or is open for reading only. A return value of EOF indicates an error. Note If fflush returns EOF, data may have been lost due to a write failure. When setting up a critical error handler, it is safest to turn buffering off with the setvbuf function or to use low-level I/O routines such as _open, _close, and _write instead of the stream I/O functions. Parameter stream Pointer to FILE structure Remarks The fflush function flushes a stream. If the file associated with stream is open for output, fflush writes to that file the contents of the buffer associated with the stream. If the stream is open for input, fflush clears the contents of the buffer. fflush negates the effect of any prior call to ungetc against stream. Also, fflush(NULL) flushes all streams opened for output. The stream remains open after the call. fflush has no effect on an unbuffered stream. Buffers are normally maintained by the operating system, which determines the optimal time to write the data automatically to disk: when a buffer is full, when a stream is closed, or when a program terminates normally without closing the stream. The commit-to-disk feature of the run-time library lets you ensure that critical data is written directly to disk rather than to the operating-system buffers. Without rewriting an existing program, you can enable this feature by linking the program’s object files with COMMODE.OBJ. In the resulting executable file, calls to _flushall write the contents of all buffers to disk. Only _flushall and fflush are affected by COMMODE.OBJ. For information about controlling the commit-to-disk feature, see Stream I/O, fopen, and _fdopen. Example /* FFLUSH.C */ #include <stdio.h> #include <conio.h> void main( void ) { int integer; char string[81]; /* Read each word as a string. */ printf( "Enter a sentence of four words with scanf: " ); for( integer = 0; integer < 4; integer++ ) { scanf( "%s", string ); printf( "%s\n", string ); } /* You must flush the input buffer before using gets. */ fflush( stdin ); printf( "Enter the same sentence with gets: " ); gets( string ); printf( "%s\n", string ); } Output Enter a sentence of four words with scanf: This is a test This is a test Enter the same sentence with gets: This is a test This is a test Stream I/O Routines See Also fclose, _flushall, setvbuf
code改为: FILE* fp = fopen(a.edf, "r"); FILE* fp1 = fopen(b.edf, "w"); while((ptr=fgets(buf,1024,fp) != NULL){ if( strstr(ptr , "comment") != NULL){ continue; } else { fprintf(fp1,"%s", ptr); } } fflush(stdin); 只需加这一行就可以吗?我试过了,也是一样的 sprintf(cmd, "ngdbuild a.edf b.edf" ); system(cmd);
赵4老师 2015-04-21
  • 打赏
  • 举报
回复
fflush Flushes a stream. int fflush( FILE *stream ); Function Required Header Compatibility fflush <stdio.h> ANSI, 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 fflush returns 0 if the buffer was successfully flushed. The value 0 is also returned in cases in which the specified stream has no buffer or is open for reading only. A return value of EOF indicates an error. Note If fflush returns EOF, data may have been lost due to a write failure. When setting up a critical error handler, it is safest to turn buffering off with the setvbuf function or to use low-level I/O routines such as _open, _close, and _write instead of the stream I/O functions. Parameter stream Pointer to FILE structure Remarks The fflush function flushes a stream. If the file associated with stream is open for output, fflush writes to that file the contents of the buffer associated with the stream. If the stream is open for input, fflush clears the contents of the buffer. fflush negates the effect of any prior call to ungetc against stream. Also, fflush(NULL) flushes all streams opened for output. The stream remains open after the call. fflush has no effect on an unbuffered stream. Buffers are normally maintained by the operating system, which determines the optimal time to write the data automatically to disk: when a buffer is full, when a stream is closed, or when a program terminates normally without closing the stream. The commit-to-disk feature of the run-time library lets you ensure that critical data is written directly to disk rather than to the operating-system buffers. Without rewriting an existing program, you can enable this feature by linking the program’s object files with COMMODE.OBJ. In the resulting executable file, calls to _flushall write the contents of all buffers to disk. Only _flushall and fflush are affected by COMMODE.OBJ. For information about controlling the commit-to-disk feature, see Stream I/O, fopen, and _fdopen. Example /* FFLUSH.C */ #include <stdio.h> #include <conio.h> void main( void ) { int integer; char string[81]; /* Read each word as a string. */ printf( "Enter a sentence of four words with scanf: " ); for( integer = 0; integer < 4; integer++ ) { scanf( "%s", string ); printf( "%s\n", string ); } /* You must flush the input buffer before using gets. */ fflush( stdin ); printf( "Enter the same sentence with gets: " ); gets( string ); printf( "%s\n", string ); } Output Enter a sentence of four words with scanf: This is a test This is a test Enter the same sentence with gets: This is a test This is a test Stream I/O Routines See Also fclose, _flushall, setvbuf

64,282

社区成员

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

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