网站首页 > 文章中心 > 其它

关于c语言中用于txt的函数的信息

作者:小编 更新时间:2023-09-23 16:00:55 浏览量:441人看过

关于C语言中读取TXT文本的内容

①.通过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语言如何读取txt文本里面的内容?

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

C语言用什么函数 读取txt

写几段代码就是了

char s[100];

FILE *fp;

fp=fopen("d:\\1.txt","r");//打开文件,注意是两个\\

以上就是土嘎嘎小编为大家整理的关于c语言中用于txt的函数的信息相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章