⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eg.cpp

📁 linux下文件的读写操作 linux下文件的读写操作
💻 CPP
字号:
/* *      eg.cpp *       *      Copyright 2009 Lee <lee@lee-laptop> *       *      This program is free software; you can redistribute it and/or modify *      it under the terms of the GNU General Public License as published by *      the Free Software Foundation; either version 2 of the License, or *      (at your option) any later version. *       *      This program is distributed in the hope that it will be useful, *      but WITHOUT ANY WARRANTY; without even the implied warranty of *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the *      GNU General Public License for more details. *       *      You should have received a copy of the GNU General Public License *      along with this program; if not, write to the Free Software *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, *      MA 02110-1301, USA. */#include <iostream>#include <stdio.h>#include <unistd.h>#include <fcntl.h>#include <stdlib.h>#include <errno.h>#include <string.h>using namespace std;#define buffer_size 1024int main(int argc,char **argv){  int from_fd,to_fd; int bytes_read,bytes_write; char buffer[buffer_size]; char *ptr;int strcont1=0;	int strcont2=0;        char path1[50]={NULL};    cout<<"请输入源文件路径:"<<endl;    cin>>path1;        char path2[50]={NULL};    cout<<"请输入目标文件路径:"<<endl;    cin>>path2;        for(int i=0;i<50;i++)    {    	if(path1[i]!=NULL)    	strcont1++;    	else     	   break;    }        for(int i=0;i<50;i++)    {    	if(path2[i]!=NULL)    	strcont2++;    	else     	   break;    }    //cout<<path1<<strcont<<endl;    char* oldpath=new char[strcont1];    char* newpath=new char[strcont2];        for(int j=0;j<strcont1;j++)    {    	oldpath[j]=path1[j];    }        for(int j=0;j<strcont2;j++)    {    	newpath[j]=path2[j];    }    //cout<<oldpath<<" and "<<newpath;/* if(argc!=3)  {fprintf(stderr,"usage:%s fromfile tofile\n\a",argv[0]);exit(1);  }    */ if((from_fd=open(oldpath,O_RDONLY))==-1)  {fprintf(stderr,"open %s error:%s\n",argv[1],strerror(errno));exit(1); } if((to_fd=open(newpath,O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR))==-1)  {        fprintf(stderr,"open %s error:%s\n",argv[2],strerror(errno));        exit(1); }  while(bytes_read=read(from_fd,buffer,buffer_size)) {   if((bytes_read==-1)) break;   else if(bytes_read>0)       {  ptr=buffer;  while(bytes_write=write(to_fd,ptr,bytes_read))   {     if((bytes_write==-1))break;     else if(bytes_write==bytes_read) break;     else if(bytes_write>0)           {      ptr+=bytes_write;   bytes_read-=bytes_write;          }          }         if(bytes_write==-1)break;       }  } close(from_fd); close(to_fd); exit(0);} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -