编写一个程序,在当前目录下创建用户可读写文件“hello.txt”,在其中写入“Hello,software weekly”,关闭该文件。再次打开该文件,读取其中的内容并输出在屏幕上。判断读写是否成功

99封情书 提交于 2020-03-02 11:30:27

1 #include<stdio.h>
2 #include<sys/types.h>
3 #include<sys/stat.h>
4 #include<fcntl.h>
5 #include<unistd.h>
6 #include<string.h>
7 #define num 50
8 int main()
9 {
10 int r_fd,w_fd,ret;
11 char s[num];
12 char c[30]=“hello,software weekly\n”;
13 w_fd=open(“hello.txt”,O_CREAT|O_RDWR,S_IRWXU);
14 if(w_fd==-1)
15 {
16 printf(“write error!”);
17 return -1;
18 }
19 else
20 {
21 write(w_fd,c,strlen©);
22 close(w_fd);
23 }
24 r_fd=open(“hello.txt”,O_RDWR);
25 ret=read(r_fd,s,num);
26 if(ret==-1)
27 {
28 printf(“read error”);
29 }
30 printf("%s\n",s);
31 close(r_fd);
32 return 0;
33 }

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!