设置标志位提取字符串

本文介绍了一个简单的文件读写程序实例,包括如何在文件中记录带有时间戳的数据,并从文件中读取这些数据进行处理。重点展示了使用C语言进行文件操作的方法,包括格式化时间并写入文件,以及从文件中按特定标识读取时间和数字。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写入文件时确保没有和标志字符相同的地方,

//写入文件时

 FILE * pfwrite = NULL;

 pfwrite = fopen("C://Documents and Settings//zrc//桌面//test//报警写文件读文件设置标志//test.txt","a+");

 if (pfwrite != NULL) {
  CString strAlarm;

  SYSTEMTIME   SysTime;  
  CString   StrCurTime;  
  ::GetLocalTime(&SysTime);  
  StrCurTime.Format("%d.%d.%d_%d:%d:%d",SysTime.wYear,SysTime.wMonth,SysTime.wDay,SysTime.wHour,SysTime.wMinute,SysTime.wSecond);
  
  int i = 555;
  strAlarm.Format("%d",i);
 
  fprintf(pfwrite,StrCurTime + " time ");
  fprintf(pfwrite,strAlarm + " number ");
  fclose(pfwrite);
 }
 

 

//读取文件时

 FILE * pfread = NULL;
 pfread = fopen("C://Documents and Settings//zrc//桌面//test//报警写文件读文件设置标志//test.txt","r");

 char temp[1000];
 char temp1[1000];
 if (pfread != NULL) {
  fscanf(pfread, "%s",temp);
  MessageBox(temp);
  fscanf(pfread, "%s",temp1);

  //提取时间
  while (feof(pfread) == 0) {
   while (1) {
    if (!strcmp(temp1,"time")) {//这里的TEMP是真正提取的东西
     //MessageBox(temp);
     break;
    }
    else
    {
     //如果不是time,就将此次提取的字符串,和上次的字符串相加起来
     strcat(temp,temp1);
     memset(temp1,0,sizeof(temp1));
     fscanf(pfread, "%s",temp1);
    }
   }
   
   fscanf(pfread, "%s",temp);
   fscanf(pfread, "%s",temp1);
   

   //提取报警宏
  
   while (1) {
    if (!strcmp(temp1,"number")) {//这时的TEMP是真正提取的东西
     //MessageBox(temp);
     break;
    }
    else
    {
     //如果不是time,就将此次提取的字符串,和上次的字符串相加起来
     strcat(temp,temp1);
     memset(temp1,0,sizeof(temp1));
     fscanf(pfread, "%s",temp1);
    }
   }
   fscanf(pfread, "%s",temp);
   fscanf(pfread, "%s",temp1);
  }
 
  
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值