现在的位置: 首页 > 综合 > 正文

C读取txt教程

2013年10月04日 ⁄ 综合 ⁄ 共 535字 ⁄ 字号 评论关闭

#include<stdio.h>
#include<string.h>

void main()
{
 char line[50];int main(int argc, char const *argv[])
 {
  /* code */
  return 0;
 }
 char name[20], ps[8];
 
 FILE *fp = fopen("User.txt","r");
 if(!fp)
 {
  return;
 }
 
 while(!feof(fp))//end return ture
 {
  memset(line,0,50);  // clean memory line
  memset(name,0,20); // clean memory name
  memset(ps,0,8); // clean ..
  fgets(line,50,fp);  // read 50 chars frome fp to line untill \t or end of file

  sscanf(line, "%s%s", name, ps);  // format input keyword space 
  printf("%s\n",line);
  printf("Name:[%s], Pass:[%s]\n",name,ps);
 }
}

这个是要读取的txt文档。

注释上面都写得有各个步骤的含义。

抱歉!评论已关闭.