我们在有些试验的时候,需要记录开发板的启动次数,看是否启动成功,用于判断是否符合标准,但是我们又不想自行人工的去数启动的次数,这里就使用一个小程序来实现
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
void help()
{
printf("Param is incorrect,Please check it \n");
printf("Usage: runtimes filename(filename path is absulute path)\n");
//printf("\nfilename : the filename you want to savefile\r\n");
return ;
}
int main(int arc, char *argv[])
{
if(arc !=2)
{
help();
exit(0);
}
//printf("argv[1]=%s\r\n",argv[1]);
FILE *fp = NULL;
char *str="";
fp = fopen (argv[1], "r");
if (fp == NULL) //file is not found,then counts=0,create file and write 0 count
{
fp = fopen (argv[1], "w+");
fprintf(fp,"PowerOn counts is 1\r\n");
printf("PowerOn counts is %d\n",1);
fclose(fp);
exit(0);
}
else
{
//calc this file have lines
int ch;
int lines=1;
while((ch = fgetc(fp)) != EOF)
{
if(ch == '\n')
lines++;
}
fclose(fp);
//reopen file and write lines+1
fp = fopen (argv[1], "a");
fprintf(fp,"PowerOn counts is %d\r\n",lines);
fclose(fp);
printf("PowerOn counts is %d\n",lines);
exit(0);
}
return 0;
}
编译成arm系统下的执行文件,拷贝到开发板中。
执行如下:
root@imx6ulevk:~# ./runtimes ~
Segmentation fault
root@imx6ulevk:~# ./runtimes ~/runtimes.log
PowerOn counts is 1
root@imx6ulevk:~# ./runtimes ~/runtimes.log
PowerOn counts is 2
root@imx6ulevk:~# ./runtimes ~/runtimes.log
PowerOn counts is 3
root@imx6ulevk:~# ./runtimes ~/runtimes.log
PowerOn counts is 4
程序执行成功,然后将这个命令放入到启动时执行的脚本中,就可以自动实现了。
具体如何将文件加入到开发板的启动序列中,查阅自己的开发板的手册或者参考我以前发的文章吧。
下面我们可能进行嵌入式系统的通讯接口的编程实现,欢迎关注。
创作不易,点个赞再走呗,期待共同提高。