温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

0301使用open函数编写一个程序

发布时间:2020-06-11 03:01:30 来源:网络 阅读:404 作者:银河星君 栏目:编程语言

使用open函数编写一个程序,打开或者创建一个指定的文件,带路径文件名由用户指定输入,文件名的最长长度为30.
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

#define FLAGS O_WRONLY|O_CREAT|O_TRUNC
#define MODE S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH

int main(int argc, char argv[])
{
const char
pathname;//指向需要打开(或创建)文件的绝对路径名或相对路径名
int fd;//文件描述符
char pn[30];//pn为字符串数组,存放要打开的文件名
printf("input the pathname[<30 strings]:");//输入路径名,小于30个字符
gets(pn);
pathname=pn;
if((fd=open(pathname,FLAGS,MODE))==-1)//调用open函数
{
printf("error,open file failed!\n");
exit(1);//出错退出
}
printf("OK,open file successful!\n");
printf("fd=%d\n",fd);
return 0;
}

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI