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 }
编写一个程序,在当前目录下创建用户可读写文件“hello.txt”,在其中写入“Hello,software weekly”,关闭该文件。再次打开该文件,读取其中的内容并输出在屏幕上。判断读写是否成功
最新推荐文章于 2023-06-01 17:23:46 发布