level 1
weijun14
楼主
/* test.c */
#include"stdio.h"
#include"sys/types.h"
#include"sys/stat.h"
#include"unistd.h"
int main(int argc,char *argv[])
{
time_t old_time=0;
struct stat bust;
int fd,pid,state;
fd=open(argv[1],0);
if(argc!=3)
{
printf("参数个数不正确!");
return 1;
}
if(fd==-1)
{
printf("Watchfile :%s can't open!",argv[1]);
return 2;
}
fstat(fd,&bust);
old_time=bust.st_mtime;
while(1)
{
fstat(fd,&bust);
if(old_time!=bust.st_mtime)
{
while((pid=fork())==-1);
if(pid==0)
{
execl("/bin/cp","/bin/cp","argv[1]","argv[2]",0);
return 0;
}
wait(&state);
old_time=bust.st_mtime;
}
else
sleep(5);
}
}
功能:对某一文件进行监视,如果它修改了,自动建立文件的备份。
代码编译能通过,但无法建立文件的备份,我用gdb单步调试发现老是报出there is no member named st_mtime 有哪位高手帮忙改一下,菜鸟在此感谢了!
2012年06月20日 14点06分
1
#include"stdio.h"
#include"sys/types.h"
#include"sys/stat.h"
#include"unistd.h"
int main(int argc,char *argv[])
{
time_t old_time=0;
struct stat bust;
int fd,pid,state;
fd=open(argv[1],0);
if(argc!=3)
{
printf("参数个数不正确!");
return 1;
}
if(fd==-1)
{
printf("Watchfile :%s can't open!",argv[1]);
return 2;
}
fstat(fd,&bust);
old_time=bust.st_mtime;
while(1)
{
fstat(fd,&bust);
if(old_time!=bust.st_mtime)
{
while((pid=fork())==-1);
if(pid==0)
{
execl("/bin/cp","/bin/cp","argv[1]","argv[2]",0);
return 0;
}
wait(&state);
old_time=bust.st_mtime;
}
else
sleep(5);
}
}
功能:对某一文件进行监视,如果它修改了,自动建立文件的备份。
代码编译能通过,但无法建立文件的备份,我用gdb单步调试发现老是报出there is no member named st_mtime 有哪位高手帮忙改一下,菜鸟在此感谢了!