linux c编程的问题

aliulf 2017-10-18 10:58:49
//每隔一秒创建一个文件夹,名称为时+分
//在文件夹中每隔一秒创建一个文件,将时间信息写入文件中
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<time.h>
#include<sys/time.h>
#include<dirent.h>
#include<string.h>
#include<stdio.h>
int creat_dir(char *); //创建文件夹函数
int creat_file(char *,char *,char *); //创建并写入函数
int creat_dir(char *pathname)
{
DIR *p;
int temp; //mkdir返回值
p=opendir(pathname);
if(p=NULL)
{
printf("打开文件夹失败,文件夹不存在\n");
temp=mkdir(pathname,0777);
if(temp==-1)
{
printf("创建文件失败\n");
return -1;
}
else
{
printf("创建文件夹成功\n");
return 0;
}
}
else
{
printf("文件夹已存在\n");
return 0;
}
}
int creat_file(char *pathname,char *filename,char *writebuf)
{
int temp; //chdir返回值
int fd; //文件标识符
int temp_write; //open函数返回值
char wdir[100]; //储存工作路径
temp=chdir(pathname);
if(temp==-1)
{
printf("切换工作路径失败\n");
return -1;
}
else
{
if((getcwd(wdir,100))==NULL)
{
printf("获取工作路径失败\n");
}
printf("工作路径为:%s\n",wdir);
fd=open(filename,O_RDWR|O_CREAT,S_IRWXU);
//以传入名字创建文件
if(fd==-1)
{
printf("创建文件失败\n");
return -1;
}
else
{
printf("创建文件成功\n");
temp_write=write(fd,writebuf,strlen(writebuf));
if(temp_write==-1)
{
printf("写入失败\n");
close(fd);
return -1;
}
else
{
printf("写入成功\n");
close(fd);
return 0;
}
}
}
}
int main()
{
int temp_creat; //创建文件夹函数返回值
int temp_file; //创建文件返回值
char dirname[10]="DIR"; //目录名
char filename[10]="File"; //文件名
char writebuf[15]; //写入缓冲区
time_t timetemp; //时间变量
struct tm *time_str; //时间结构体
char timebuf[3]; //待连接入的时间信息
char timebuf_2[3];
time(&timetemp);
time_str=gmtime(&timetemp); //存入结构体时间中
sprintf(timebuf_2,"%02d",time_str->tm_min);
strcat(dirname,timebuf_2); //文件夹名称加入时间信息
temp_creat=creat_dir(dirname); //调用创建文件夹函数
if(temp_creat==-1) //创建失败
{
return 1;
}
else
{

sprintf(writebuf,"%s\n",ctime(&timetemp)); //将时间信息加入到writebuf中
sprintf(timebuf,"%02d",time_str->tm_sec);
strcat(filename,timebuf); //文件名称加入时间信息
temp_file=creat_file(dirname,filename,writebuf);
//调用创建文件函数
if(temp_file==-1) //创建失败
{
return 2;
}
else
{
return 0;
}
}
}


运行结果是
文件夹已存在
切换工作路径失败
已放弃(核心转储)

我gdb调试了下 打开文件夹那里 明明文件夹不存在 为什么返回的指针p不是NULL 纠结了很久 新手 求大神指点



...全文
2763 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
suxingtian 2019-06-01
  • 打赏
  • 举报
回复
老手不会写错
圣骑士笑笑 2018-11-27
  • 打赏
  • 举报
回复
这个可以的, if里面的语法问题
英雄@末路 2018-10-09
  • 打赏
  • 举报
回复
if(p) 就行了,现在编译器都支持,如果记得NULL == p 当然好,但一般想得起这么写,那估计正向写也不会忘了。。。
Chelios_Lee 2018-08-22
  • 打赏
  • 举报
回复
我也觉得用shell 几行就搞定了 ,或者在程序中直接用system调用
理科男同学 2018-08-19
  • 打赏
  • 举报
回复
哈哈哈!受教了
Leoyyd 2018-02-28
  • 打赏
  • 举报
回复
很实用的技巧,学习了!
念念有余 2018-02-05
  • 打赏
  • 举报
回复
引用 8 楼 windandcloudflying 的回复:
if判断这样写 if (NULL == p) 这样可以防止比较符号写成赋值符号
这个建议不错
u012236270 2018-01-17
  • 打赏
  • 举报
回复
所以所 if判断还是需要if(NULL == p) 比较安全点
yuedahan 2017-11-22
  • 打赏
  • 举报
回复
全是坑啊。不是在改BUG就是在写BUG
Acuity. 2017-11-20
  • 打赏
  • 举报
回复
在CSDN遇到很多把“=”当成“==”的情况了。
  • 打赏
  • 举报
回复
楼上的建议一看就是老手了
永远的霸者 2017-11-11
  • 打赏
  • 举报
回复
if判断这样写 if (NULL == p) 这样可以防止比较符号写成赋值符号
lininglive 2017-11-10
  • 打赏
  • 举报
回复
这错误比较低级,但是是经常犯的
yangtuo250 2017-11-09
  • 打赏
  • 举报
回复
用shell写更简单吧
aliulf 2017-11-09
  • 打赏
  • 举报
回复
引用 2 楼 bigPillow的回复:
Hi Feng, In your`s code : if(p=NULL) you mean to be: if(p==NULL)
yes thanks
aliulf 2017-11-09
  • 打赏
  • 举报
回复
引用 3 楼 HITYM627的回复:
楼主现在这个问题解决了吗 能否分享一下
解决了 就是楼上说的那样
HITYM627 2017-11-03
  • 打赏
  • 举报
回复
楼主现在这个问题解决了吗 能否分享一下
bigPillow 2017-10-22
  • 打赏
  • 举报
回复
Hi Feng, In your`s code : if(p=NULL) you mean to be: if(p==NULL)
jklinux 2017-10-19
  • 打赏
  • 举报
回复
先用管理员权限执行程序试看看,程序不一定有权限创建目录或文件的。

1,318

社区成员

发帖
与我相关
我的任务
社区描述
主要是开发驱动技术
社区管理员
  • 驱动程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧