①.通过fopen函数打开文本,例如file
*fp=fopen("in.txt","r");//返回一个file类型的句柄
关闭已经打开的文件.
#include
int main()
{
int data;
file *fp=fopen("in.txt","r");
if(!fp)
printf("can't open file\n");
return -1;
}
while(!feof(fp))
fscanf(fp,"%d",data);
printf("\n");
fclose(fp);
return 0;
C语言可以使用fopen()函数读取txt文本里.
示例:
#include stdio.h
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 */
/* Close stream */
/* All other files are closed: */
numclosed = _fcloseall( );
printf( "Number of files closed by _fcloseall: %u\n", numclosed );
扩展资料
使用fgetc函数
#include stdlib.h
FILE *stream;
int? i, ch;
/* Open file to read line from: */
if( (stream = fopen( "fgetc.c", "r" )) == NULL )
exit( 0 );
ch = fgetc( stream );
buffer[i] = (char)ch;
/* Add null to end string */
buffer[i] = '\0';
printf( "%s\n", buffer );
fclose( stream );
写几段代码就是了
char s[100];
FILE *fp;
fp=fopen("d:\\1.txt","r");//打开文件,注意是两个\\
以上就是土嘎嘎小编为大家整理的关于c语言中用于txt的函数的信息相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!