注:data必须在工程目录下新建一个data.txt文件,不然打不开,因为data是只读模式!!不能像data2一样写入!
/* FOPEN.C: This program opens files named "data" * and "data2".It uses fclose to close "data" and * _fcloseall to close all remaining files. */ #include <stdio.h> FILE *stream, *stream2; void main(void) { int numclosed; stream = fopen("data.txt", "r"); stream2 = fopen("data2.txt", "w"); /* open for read (will fail if file "data" does not exist) */ if ((stream = fopen("data", "r")) == NULL) printf("the file 'data' was not opened\n"); else printf("the file 'data' was opened\n"); /* open for write */ if ((stream2 = fopen("data2", "w+")) == NULL) printf("the file 'data2' was not opened\n"); else printf("the file 'data2' was opened\n"); /* close stream */ if (fclose(stream)) printf("the file 'data' was not closed\n"); /* all other files are closed: */ numclosed = _fcloseall(); printf("number of files closed by _fcloseall: %u\n", numclosed); }写完发现 有stream和stream2,要用上啊,太菜了:
又发现if里面用了stream和stream2,太菜菜了:、
/* FOPEN.C: This program opens files named "data" * and "data2".It uses fclose to close "data" and * _fcloseall to close all remaining files. */ #include <stdio.h> FILE *stream, *stream2; void main( void ) { int numclosed; /* Open for read (will fail if file "data" does not exist) */ if( (stream = fopen( "data", "r" )) == NULL ) printf( "The file 'data' was not opened\n" ); else printf( "The file 'data' was opened\n" ); /* Open for write */ if( (stream2 = fopen( "data2", "w+" )) == NULL ) printf( "The file 'data2' was not opened\n" ); else printf( "The file 'data2' was opened\n" ); /* Close stream */ if( fclose( stream ) ) printf( "The file 'data' was not closed\n" ); /* All other files are closed: */ numclosed = _fcloseall( ); printf( "Number of files closed by _fcloseall: %u\n", numclosed ); }