600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > c语言中以只读方式打开 C 中如何以只读方式打开文件

c语言中以只读方式打开 C 中如何以只读方式打开文件

时间:2024-07-04 05:07:20

相关推荐

c语言中以只读方式打开 C  中如何以只读方式打开文件

fopen( "data", "r" );这样就行了。下面是一个完整代码的例子。

(取自微软MSDN)

#include 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

" ); ? else ? ? ?printf( "The file 'data' was opened

" ); ? /* Open for write */ ? if( (stream2 = fopen( "data2", "w " )) == NULL ) ? ? ?printf( "The file 'data2' was not opened

" ); ? else ? ? ?printf( "The file 'data2' was opened

" ); ? /* Close stream */ ? if( fclose( stream ) ) ? ? ?printf( "The file 'data' was not closed

" ); ? /* All other files are closed: */ ? numclosed = _fcloseall( ); ? printf( "Number of files closed by _fcloseall: %u

", numclosed );}

Output

The file 'data' was openedThe file 'data2' was openedNumber of files closed by _fcloseall: 1。

全部

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。