- 论坛徽章:
- 0
|
#define T_INTERVAL 1
int tick_ok = 0;
void sigalrm_fn(int sig)
{
tick_ok = 1;
alarm(T_INTERVAL);
return;
}
int main(int argc, char *argv[])
{
int sys_retval;
FILE *hfile_fp;
unsigned char *data_ptr;
/* for time */
struct tm *local;
time_t ltime;
int old_min, old_hour;
unsigned long ll=0;
data_ptr = (unsigned char *)malloc(4096 * sizeof(unsigned char));
if (data_ptr==NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
memset(data_ptr, 'A', 4096 * sizeof(unsigned char));
/* to determine whether /sdcard exists */
system("mount -t vfat /dev/mmc/blk0/part1 /mnt");
sleep(10);
sys_retval = access("/mnt/mntfailed", F_OK);
if (sys_retval == 0) {
printf("mount SD card failed!!!\n");
exit(EXIT_FAILURE);
}
printf("mnt sd successed!\n");
//创建/打开文件,截断方式
if ((hfile_fp= fopen("/mnt/abc.txt", "wb+"))==NULL) {
printf("creat flie abc.txt error!\n");
exit(EXIT_FAILURE);
}
ltime = time(NULL);
local = localtime(<ime);
old_min = local->tm_min;
signal(SIGALRM, sigalrm_fn);
alarm(T_INTERVAL);
printf("history datas!\n");
while (1) {
if (tick_ok) {
tick_ok = 0;
ltime = time(NULL);
local = localtime(<ime);
//写文件
if (fwrite(data_ptr, sizeof(char), 1024, hfile_fp) != 1024) {
printf("write file abc.txt error!\n");
exit(EXIT_FAILURE);
}
printf("%d\n", ++ll);
/* 新的一分钟到来 */
if(local->tm_min != old_min) {
//printf("newm:%d, oldm:%d\n", local->tm_min, old_min);
printf("new min!\n");
old_min = local->tm_min;
if (fclose(hfile_fp) != 0) {
printf("close file error!\n");
exit(EXIT_FAILURE);
}
usleep(100000);
/* 打开文件 */
if ((hfile_fp=fopen("/mnt/abc.txt", "ab+")) == NULL) {
printf("open flie abc.txt error!\n");
exit(EXIT_FAILURE);
}
}
}
usleep(50000);
}
free(data_ptr);
if (fclose(hfile_fp) != 0) {
printf("close file error!\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
} |
|