c程序按行读取文件

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

#define FNAME "./xeyez.txt"
#define MAXSIZE 1024
int iCaltall;
int icount;

int savetokey(char *buf, char *buftime, char *bufindex)
{
 //printf(".......222222\n");
 int i = 0;
 int count = 0;
 char buftmp[50];
 //printf("buf:%s\n", buf);
 while(buf[i] != '\0')
 {

  if (buf[i] == 't' && buf[i+1] == 'i')
  {
   //printf("entry time\n");
   //buftime = buf + i + 4;
   strcpy(buftmp , buf+i+5);
   while(buftmp[count] != ',')
   {
    count++;
   }
   //printf("count:%d\n", count);
   //buftmp[count] = '\0';
   strncpy(buftime, buftmp, count);
   buftime[count] = '\0';
   printf("buftime:%s\n", buftime);
  }

  if (buf[i] == 'i' && buf[i + 1] == 'n')
  {
   //printf("entry index\n");
   //bufindex = buf + i + 5;
   strcpy(bufindex, buf+i+6);
   printf("bufindex:%s\n", bufindex);
  }
  i++;
 }
 return 0;
}
int readline()
{
 FILE *fp;
 long timeA, timeB;
 int dTime = 0;
 char bufA[MAXSIZE];
 char buf[MAXSIZE];
 char bufB[MAXSIZE];
 char buftimeA[50];
 char buftimeB[50];
 char bufindexA[20];
 char bufindexB[20];
 char buftime[50];
 char bufindex[20];
 if ((fp = fopen(FNAME, "r")) == NULL)
 {
  printf("fopen err!\n");
 }
 while(fgets(bufA, MAXSIZE, fp) != NULL)
 {
  //savetokey(bufA, buftimeA, bufindexA);
  //fgets(bufA, MAXSIZE, fp);
  //strcpy(bufA, buf);
  //savetokey(bufA, buftimeA, bufindexA);
  //fgets(bufB, MAXSIZE, fp);
  //savetokey(bufA, buftimeB, bufindexB);
  savetokey(bufA, buftime, bufindex);
 
  if(!strcmp(bufindexA, bufindex))
  {
   timeA = atol(buftimeA);
   timeB = atol(buftime);
   dTime = timeB - timeA;
   iCaltall += dTime;   
   printf("dTime:%d, count:%d\n", dTime, icount);
  }
  strcpy(buftimeA , buftime);
  strcpy(bufindexA, bufindex);
  //iCaltall += dTime;
  icount++;
 }
 //iCaltall = iCaltall - dTime;
 printf("tall: icaltall:%d, statical:%d, icount:%d\n", iCaltall, (iCaltall/icount), icount);
 return 0;
}
int main(int argc, char *argv[])
{
 iCaltall = 0;
 icount = 0;
 //printf(".......1111111\n");
 readline();
 return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值