手抄报 安全手抄报 手抄报内容 手抄报图片 英语手抄报 清明节手抄报 节约用水手抄报

c 如何读取txt文件

时间:2024-10-13 02:16:40

1、使用VS新建空工程,直接点击确定,如下所示。

c  如何读取txt文件

2、新建c文件,用于C语言编译器,输入main.c文件,如下所示。

c  如何读取txt文件
c  如何读取txt文件

3、参考代码:#include <stdio.h>#inclu蟠校盯昂de <stdlib.h>稆糨孝汶;#include <string.h>#define MAX_LINE 1024void ReadTxt(char* pFilePath){ char buf[MAX_LINE]; /*缓冲区*/ FILE *fp; /*文件指针*/ int len; /*行字符个数*/ if((fp = fopen(pFilePath,"r")) == NULL) { perror("fail to read"); exit (1) ; } while(fgets(buf,MAX_LINE,fp) != NULL) { len = strlen(buf); buf[len-1] = '\0'; /*去掉换行符*/ printf("%s %d \n",buf,len - 1); } return ;}int main(){ ReadTxt("D:\\test.txt"); //读取TXT文件函数 getchar(); return 0;}

4、将上述参考代码,复制到main.c文件内,直接编译即可。

c  如何读取txt文件

5、编译完成后,运行exe程序,执行后显示console程序,如下所示。

c  如何读取txt文件
© 手抄报圈