首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

看看这是咋回事?

2013-07-16 
看看这是怎么回事??代码如下:#includestdlib.h#includestdio.h#includestring.h#includetime.h#in

看看这是怎么回事??
代码如下:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<time.h>
#include<unistd.h>
#include<fcntl.h>
#include<signal.h>

int main(void)
{
time_t now;
struct tm *timenow;
pid_t pid = -1;
int fd = -1;
char * str = malloc(32 * sizeof(char));

if((pid = fork()) < 0)
{
perror("fork");
exit(-1);
}
if(pid > 0)
{
exit(0);
}
else
{
setsid();
if(chdir("/") < 0)
{
perror("chdir");
exit(-1);
}
close(0);
if(open("dev/null", O_RDWR) < 0)
{
perror("open dev/null");
exit(-1);
}

fd = open("time", O_RDWR|O_CREAT, 0644);
if(fd < 0)
{
perror("open time");
exit(-1);
}
dup2(0, 1);
dup2(0, 2);
while(1)
{
time(&now);
timenow = localtime(&now);
str = asctime(timenow);
write(fd, str, strlen(str));
sleep(5);
}
close(fd);

}


return 0;
}

生成的time文件用vi编辑器打开之后,保存关闭,读取到的时间就写不进time文件里了,不知道是什么么,我想知道这涉及到了哪里才导致写不进文件的?是权限问题还是写缓冲问题?请各位大神帮忙,谢谢!! 文件,缓冲,权限
[解决办法]
写不进是什么意思? open 出错, 还是 write 出错, 还是都没出错, 只是内容不是写的内容?
[解决办法]
查一下权限是否被改变。

引用:
代码如下:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<time.h>
#include<unistd.h>
#include<fcntl.h>
#include<signal.h>

int main(void)
{
time_t now;
struct tm *timenow;
pid_t pid = -1;
int fd = -1;
char * str = malloc(32 * sizeof(char));

if((pid = fork()) < 0)
{
perror("fork");
exit(-1);


}
if(pid > 0)
{
exit(0);
}
else
{
setsid();
if(chdir("/") < 0)
{
perror("chdir");
exit(-1);
}
close(0);
if(open("dev/null", O_RDWR) < 0)
{
perror("open dev/null");
exit(-1);
}

fd = open("time", O_RDWR
[解决办法]
O_CREAT, 0644);
if(fd < 0)
{
perror("open time");
exit(-1);
}
dup2(0, 1);
dup2(0, 2);
while(1)
{
time(&now);
timenow = localtime(&now);
str = asctime(timenow);
write(fd, str, strlen(str));
sleep(5);
}
close(fd);

}


return 0;
}

生成的time文件用vi编辑器打开之后,保存关闭,读取到的时间就写不进time文件里了,不知道是什么么,我想知道这涉及到了哪里才导致写不进文件的?是权限问题还是写缓冲问题?请各位大神帮忙,谢谢!!

热点排行