图书馆新生入馆培训:在c语言中怎样打开一个文本文档

来源:百度文库 编辑:查人人中国名人网 时间:2024/05/05 20:15:15

#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 );
}

Output

The file 'data' was opened
The file 'data2' was opened
Number of files closed by _fcloseall: 1

如果你的意思是在Windows系统下用系统自带的程序打开文本文档,而不是显示在控制台屏幕上的话,可以用以下命令:
system("\"C:\\文件夹名\\文件名.txt\"");
注意要包含windows.h头文件,即:在首行加一句
#include<windows.h>
这样系统就会调用记事本打开文本文件。