IT

파일 복사하기

kio467 2014. 5. 14. 23:26

#include <stdio.h>

#include <unistd.h>

#include <sys/stat.h>

#include <sys/types.h> 

#include <fcntl.h>  

#include <stdlib.h> 

#define SIZE 100


int main()

{

char enter1[SIZE] = "Enter a file name to read : ";

char success1[SIZE] = "File open 1 : Success!\n";

char enter2[SIZE] = "Enter a file name to write : ";

char success2[SIZE] = "File open 2 : Success!\n";


char filename1[SIZE], filename2[SIZE];

char buffer[SIZE];

int file1, file2;

int name;

int filestate;


write(1, enter1, SIZE);

name = read(1,filename1,SIZE);

filename1[name-1]='\0';


if((file1 = open(filename1, O_RDONLY, 0644)) == -1)

{

perror("open failed : 1");

exit(1);

}


write(1, success1, SIZE);

write(1, enter2, SIZE);


name = read(1,filename2,SIZE);

filename2[name-1] = '\0';


if((file2 = open(filename2, O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1)

{

perror("open failed : 2");

exit(1);

}


while((filestate = read(file1, buffer, SIZE)) > 0)

{

write(file2, buffer, filestate);

}


close(file1);

close(file2);


}