日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

操作系统上机作业--使用系统调用实现mycp

發布時間:2023/12/1 windows 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 操作系统上机作业--使用系统调用实现mycp 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  • mycp.c的功能與系統cp程序相同
  • 將源文件復制到目標文件,例子如下:
  • 要求使用系統調用open/read/write/close實現
$ cat /etc/passwd root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin ... $ ./mycp /etc/passwd passwd.bak $ cat passwd.bak root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin

實現思路:從main中接受mycp的參數,open源文件,用stat獲取文件大小,read源文件,open目標文件,將從源文件中讀取的字符寫入目標文件中。
實現代碼

#include<stdio.h> #include<stdlib.h>#include<sys/types.h> #include<unistd.h>#include<sys/stat.h> #include<fcntl.h>int main(int argc,char *argv[]){int fd;int fsize;char *buffer;struct stat st;if(argc!=3){printf("Error:parameter wrong!\n");exit(0);}fd=open(argv[1],O_RDONLY);if(fd<0){printf("Error:can't open the read-file!\n");exit(0);}stat(argv[1],&st);fsize=st.st_size;buffer=(char *)malloc((1+fsize)*sizeof(char));if(!buffer){printf("Error:memory wrong!\n");exit(0);}read(fd,buffer,fsize);close(fd);fd=open(argv[2],O_WRONLY|O_CREAT);if(fd<0){printf("Error:can't open the write-file!\n");exit(0);}write(fd,buffer,fsize);close(fd);free(buffer);return 0; }

運行結果

歡迎留言交流。。。。

總結

以上是生活随笔為你收集整理的操作系统上机作业--使用系统调用实现mycp的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。