|
|
您好!请教一个问题:
我用以下代码建立一个文件,并写入一些数据,结果打开文件一看,最后莫名其妙多出来一个0xFF,请问这是怎么回事?谢谢!
#include "stdio.h"
#include "time.h"
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
static unsigned char timebuff[20],filename[22],endflg=0xFF,begintime[8];
static unsigned char filehead[14]={0x55,0xAA,0x55,0xAA,'A','B','C','D','E','F','1','2','3','4'};
static unsigned char serialno[12]={'S','N','0','0','0','1','V','E','R','1','0','0'};
static time_t t;
static struct tm* tm;
static int fd;
static void NewFile(void)
{
int i;
unsigned char *p;
time(&t);
tm =localtime(&t);
sprintf(filename,"/%02d%02d%02d%02d%02d%02d.dat",tm->tm_year-100,1+tm->tm_mon,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
if((fd=open(filename,O_RDWR | O_CREAT,S_IRUSR | S_IWUSR))==-1)
{
// fprintf(stderr,"Open %s Error %s\n",filename,strerror(errno));
exit(1);
}
write(fd,filehead,14);
write(fd,serialno,12);
write(fd,&endflg,1);
begintime[0]=tm->tm_year-100;
begintime[1]=1+tm->tm_mon;
begintime[2]=tm->tm_mday;
begintime[3]=tm->tm_hour;
begintime[4]=tm->tm_min;
begintime[5]=tm->tm_sec;
begintime[6]=1/100;
begintime[7]=1-1/100*100;
write(fd,begintime,8);
write(fd,&endflg,1);
write(fd,&endflg,1);
close(fd);
// if((fd=open(filename,O_RDWR | O_APPEND,S_IRUSR | S_IWUSR))==-1)
{
// fprintf(stderr,"Open %s Error %s\n",filename,strerror(errno));
// exit(1);
}
return NULL;
}
int main(void)
{
NewFile();
} |
|