導航:首頁 > 文件教程 > c拷貝文件linux

c拷貝文件linux

發布時間:2021-12-04 13:35:26

linux C 通過管道實現文件復制

#include"stdio.h"
#include"stdlib.h"
#include"unistd.h"
#include"sys/types.h"
#include"sys/stat.h"
#include"string.h"
#include"fcntl.h"
#include"errno.h"

#define FIFO1 "/tmp/fifo"
#define MAXLINE 100

void client1(int);
void client2(int);

int main(int argc,char **argv)
{
int writefd;
writefd=open(FIFO1,O_WRONLY,0);
client1(writefd);
client2(writefd);

close(writefd);
unlink(FIFO1);
exit(0);
}

void client1(int writefd1)//實現從文件寫到管道,自己創建一個aa.txt文件
{
char buff[MAXLINE];
int fd;
memset(buff,0,sizeof(buff));
fd=open("aa.txt",O_CREAT|O_RDWR,S_IRWXU);
read(fd,buff,sizeof(buff));
write(writefd1,buff,sizeof(buff));
close(fd);
}
void client2(int writefd2)//)//實現從管道寫到另一個文件
{
char buff[MAXLINE];
int fd;
fd=open("bb.txt",O_CREAT|O_RDWR,S_IRWXU);
read(writefd2,buff,sizeof(buff));
write(fd,buff,sizeof(buff));
close(fd);

}

我這里省略了許多判斷,自己加哈
如有問題,在線交流

❷ 如何在linux下用c編程實現文件的復制

#include<stdio.h> #include<unistd.h> #include<sys/stat.h> #include<sys/types.h> #include<fcntl.h>
int main(int argc,char **argv) { if(argc<3)//參數少於3,給出提示並退出 {
printf("請按如下格式運行!\n"); printf("./a.out [原始圖片] [目標圖片]\n");
return 0;
}
int fd1 = open(argv[1],O_RDONLY);//只讀方式打開源文件 if(-1 == fd1) { perror("打開文件失敗");

return 0;
} int fd2 = open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0666);//讀寫方式打開文件,有則清空,沒有則創建 char buf[100]={0}; int res;
while(res = read(fd1,buf,sizeof(buf)))//從源文件讀出 {
write(fd2,buf,res);//寫入到目標文件
}
close(fd1);//關閉文件 close(fd2); return 0;
}

❸ linux下把文件A拷貝到文件夾B並重命名為C(保留文件A),命令怎麼寫謝謝

cp A /B/C

❹ linux c++ 拷貝文件問題

Linux下C語言實現文件拷貝

/*
Function: file from file1 to file2
How to execute: ./file file1 file2 (under Linux)
Data:2007-05-09
*/

#include <stdio.h> /*fprintf(),stderr,BUFSIZ*/
#include <stdlib.h> /**/
#include <string.h> /*stderror()*/
#include <fcntl.h> /*open(),flag*/
#include <errno.h> /*errno*/
#include <unistd.h> /*ssize_t*/
#include <sys/types.h>
#include <sys/stat.h> /*mode_t*/

#define BUFFER_SIZE 3

int main(int argc,char **argv)
{
int from_fd,to_fd;
int bytes_read,bytes_write;
char buffer[BUFFER_SIZE];
char *ptr;

if(argc!=3)
{
fprintf(stderr,"Usage:%s fromfile tofile\n\a",argv[0]);
exit(1);
}

/* 打開源文件 */
if((from_fd=open(argv[1],O_RDONLY))==-1) /*open file readonly,返回-1表示出錯,否則返迴文件描述符*/
{
fprintf(stderr,"Open %s Error:%s\n",argv[1],strerror(errno));
exit(1);
}

/* 創建目的文件 */
/* 使用了O_CREAT選項-創建文件,open()函數需要第3個參數,
mode=S_IRUSR|S_IWUSR表示S_IRUSR 用戶可以讀 S_IWUSR 用戶可以寫*/
if((to_fd=open(argv[2],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)&&(errno!=EINTR))
break;
else if(bytes_read>0)
{
ptr=buffer;
while(bytes_write=write(to_fd,ptr,bytes_read))
{
/* 一個致命錯誤發生了 */
if((bytes_write==-1)&&(errno!=EINTR))
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);
return;
}

❺ linux怎麼復制文件 c實現

ctrl+c,ctrl+v,不用shift,還有如果不是以root身份登錄,是不能在主文件夾以外的地方粘貼的

❻ linux拷貝文件a.c到home目錄下,並且不用確認

1 rm -f a.c
2 pwd
3 find / -name hello.c
4 mv a.c b.c
5 強制保存退出 顯示行號

閱讀全文

與c拷貝文件linux相關的資料

熱點內容
無保qq申訴上保 瀏覽:215
蘋果6splussd卡 瀏覽:802
庫樂隊app顯示19是什麼意思 瀏覽:701
言言直播的官方網站是多少 瀏覽:340
啄木鳥十大電影女同天堂 瀏覽:507
iris女主扮演者 瀏覽:144
51自學網cor視頻教程 瀏覽:243
怎麼用ps把臉p黑教程 瀏覽:223
播放iso藍光原盤app有哪些 瀏覽:115
尋呼機地址碼編程是什麼意思 瀏覽:947
數據包安全未知為什麼解壓不出來 瀏覽:69
愛人女主角 瀏覽:623
古龍小說下載 瀏覽:242
linux單詞統計 瀏覽:200
iphone音符符號 瀏覽:649
女設計師幾百年不死韓國 瀏覽:245
linux無法生成gbk文件 瀏覽:590
免費的最新電影qq群 瀏覽:83
數控g76螺紋怎麼編程 瀏覽:779
哪個影院不需要VIP 瀏覽:706

友情鏈接