主要通过运用open,read,write函数实现将txt文件内容复制到另一个txt文件。
#include#include #include #include #include #include #define MAX_READ 99999 int main() { size_t numRead,numWrite;//存储字符串长度 char buffer[MAX_READ+1];//存储字符串 printf("please make CPU_usage.txt be copied to target.txt!n"); int fdin,fdout;//定义两个文件指针,因为要对两个文件实施操作 //打开和错误判断 if((fdin=open("CPU_usage.txt",O_RDONLY))==-1) { printf("source file cannot open!n"); exit(0); } if ((fdout=open("target.txt", O_WRONLY))==-1) { printf("target file cannot open!n"); exit(0); } //read执行成功返回字符串长度 numRead=read(fdin,buffer,MAX_READ); if(numRead==-1) { printf("read falsen"); exit(0); } buffer[numRead]=' ';//追加结束符 printf("%s",buffer); //成功返回长度 numWrite=write(fdout,buffer,numRead+1); if(numWrite==-1) { printf("write falsen"); exit(0); } printf("copy successnplease check in target.txt:)n"); //关闭文件 close(fdin); close(fdout); return 0; }



