c 如何读取txt文件

 时间:2026-02-14 14:02:57

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

c  如何读取txt文件

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

c  如何读取txt文件

c  如何读取txt文件

3、参考代码:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX_LINE 1024

void 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文件

  • 在Excel2010中如何隐藏显示工作表
  • ppt怎么快速定位幻灯片
  • 货币定义
  • Excel:最常用的SUM函数
  • 如何插入visio甘特图的任务栏
  • 热门搜索
    浙江财经大学怎么样 海洋生物大全 金税盘怎么抄税 火影忍者游戏大全 怎么防晒 移动短号怎么开通 七妹导航大全 英雄联盟英雄图片大全 搞笑歇后语大全 瑜伽体式大全