c语言学习笔记

#include <stdio.h>
#include <time.h>

int main(void)
{
time_t t;               //类似于size_t那样的专门存时间戳的数据类型
struct tm *p;         //引入 time.h后,可以得到结构体 struct tm tm里有年月日时分秒等参数

time(&t);      // 把时间戳放到容器里,
p=localtime(&t);     // 把时间戳放到 localtime里返回结构体指针用p接收
          // 存数据需要对应的容器,返回结果需要对应的容器(数据类型)来接收。          

FILE *f;   //文件型指针

f=fopen("time.txt","w"); // 用w只写模式打开要注意,如果该文件已经存在会把该文件内容清空
if(f==NULL){
  printf("文件打开失败 ");
}else{
  fprintf(f,"%d-%d-%d",p->tm_year+1900,p->tm_mon+1,p->tm_mday);
  fclose(f);
}

//用fscanf 读一个文件里的数据实例

FILE *rf;

rf=fopen("log.dat","r");

if(rf==NULL){

  printf("文件打开失败 ");

}else{

  char name[20];

  int height;

  while(fscanf(rf,"%s%d",name,&height)==2){ //这个2是表示成功得到的参数,根据实际情况写,并不是定死的, 注意这里字符型name不用加 & 

    printf("name is %s ,height is %d",name,height); 

  }

  fclose(rf);

}

  //fgetc 把数据从文件里读出来输出  

  FILE *nf;

  nf=fget("hello.txt","r");  

  while( (ch=fgetc(nf) )!=EOF )     // getchar 获取标准输入流 

    putchar(ch);          //放到标准输出流中

  fclose(nf);

  return 0;
}

原文地址:https://www.cnblogs.com/luckylihuizhou/p/6479958.html