일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- error
- 깃 토큰
- 파이썬
- console창
- error 해결
- php
- localhost
- cmd
- 에러
- jupyter
- github token
- 데이터베이스
- visualstudio code
- DataGrip
- run sql script
- vscode
- 단축키
- github clone
- Visual Studio Code
- 오류
- csv
- Python
- OrCAD 다운로드
- import data
- MySQL
- 따옴표 삭제
- database
- PHPStorm
- 클론
- clone
Archives
- Today
- Total
개발 노트
10/13 동시에 2개 파일로 복사하는 코드, 앞 내용 절반 뒷 내용 절반씩 나누어 복사하는 코드 본문
학부 공부 : 20.08.31~12.10/Linux
10/13 동시에 2개 파일로 복사하는 코드, 앞 내용 절반 뒷 내용 절반씩 나누어 복사하는 코드
hayoung.dev 2020. 10. 14. 11:371. copy3.c : a.txt를 b.txt c.txt로 동시에 2개의 파일로 복사하는 프로그램
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
int main(int argc, char **argv)
{
int n, in, out1, out2;
char buf[1024];
char *msg = "Usage : copy3 file1 file2 file3\n";
if (argc < 4) {
write(2, msg, strlen(msg));
return -1;
}
if ((in = open(argv[1], O_RDONLY)) < 0) {
perror(argv[1]);
return -1;
}
if ((out1 = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR)) < 0) {
perror(argv[2]);
return -1;
}
if ((out2 = open(argv[3], O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR)) < 0) {
perror(argv[3]);
return -1;
}
while ((n = read(in, buf, sizeof(buf))) > 0) {
write(out1, buf, n);
write(out2, buf, n);
}
close(out2);
close(out1);
close(in);
return 0;
}
2. copyh.c : a.txt의 내용 앞 절반을 b.txt에 나머지를 c.txt로 동시에 2개의 파일로 복사하는 프로그램
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int n, in, out1, out2;
char buf[1];
struct stat sb;
char *msg = "Usage : copyh orig firsthalf lastalf\n";
if (argc < 4) {
write(2, msg, strlen(msg));
return -1;
}
if ((in = open(argv[1], O_RDONLY)) < 0) {
perror(argv[1]);
return -1;
}
if (fstat(in, &sb) == -1) {
perror("stat");
exit(EXIT_FAILURE);
}
printf("File size: %lld bytes\n", (long long) sb.st_size); // sb.st_size 가 우리가 알려는 사이즈
if ((out1 = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR)) < 0) {
perror(argv[2]);
return -1;
}
if ((out2 = open(argv[3], O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR)) < 0) {
perror(argv[3]);
return -1;
}
int cnt = 0;
while ((n = read(in, buf, sizeof(buf))) > 0) {
if(cnt < (sb.st_size/2))
write(out1, buf, n);
else
write(out2, buf, n);
cnt=cnt+n;
printf("%d ", cnt);
}
close(out2);
close(out1);
close(in);
return 0;
}
반응형
'학부 공부 : 20.08.31~12.10 > Linux' 카테고리의 다른 글
9/22 p.81 gcc, makefile 파일 (0) | 2020.09.22 |
---|---|
9/16 p.67 Linux 에서 tar로 파일 묶기, 파일 확인, 파일 묶음 풀기 실습 (0) | 2020.09.19 |
9/8 p.53 리눅스의 계층적 구조 (0) | 2020.09.08 |
9/1 p.55 sudo로 권한 다루기 (0) | 2020.09.01 |