출력
#include <unistd.h>
ssize_t write (int filedes, const void *buf, size_t nbytes);
지정된 파일에 데이터를 쓰는 함수
성공하면, 파일에 쓴 데이터의 바이트 수, 실패하면 -1을 반환
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
31
32
33
|
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char **argv)
{
int n, in, out;
char buf[1024];
if(argc !=3 ) {
write(2, "Usage: append file1 file2\n",26);
return -1;
}
if((in=open(argv[1],O_RDONLY))<0){
perror(argv[1]);
return -1;
}
if((out=open(argv[2],O_TRUNC| O_APPEND))<0){
perror(argv[2]);
return -1;
}
while((n=read(in,buf,sizeof(buf)))>0)write(out,buf,n);
close(out);
close(in);
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs |
읽기/쓰기 오프셋의 위치지정
오프셋 조정
#include <sys.types.h>
#include <unistd.h>
off_t lseek(int filedes, off_t offset, int whence);
파일의 현재 offsetㅇ들 임의의 위치로 조정
성공하면 이동한 지점의 offset, 실패하면 -1을 반환
파일 디스크립터 복사
파일의 공유
#include <unistd.h>
int dup(int filedes);
int dup2(int filedes,int filedes2);
사용 중인 파일 디스크립터를 복사해서 반환
성공하면 할당 받은 파일 디스키립터 번호, 실패하면 -1을 반환
파일 테이블의 해당 항목의 참조 계수를 1증가 시킨다.
파일 디스크립터 항목의 플래그(FD_CLOEXEC)는 초기값 (0)으로 설정된다.
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
|
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
main()
{
int fd1, fd2, cnt;
char buf[30];
fd1=open(fname,O_RDONLY);
if(fd1<0){
perror("open()");
exit(-1);
}
fd2=dup(fd1);
cnt = read(fd1,buf,12);
buf[cnt]='\0';
printf("fd1's printf: %s(%d)\n",buf,fd1);
lseek(fd1,1,SEEK_CUR);
cnt=read(fd2,buf,12);
buf[cnt] ='\0';
printf("fd2's printf: %s(%d)\n",buf,fd2);
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
int fd;
if((fd=creat(fname,0666))<0){
perror("creat()");
return -1;
}
printf("First printf is on the screen.\n");
dup2(fd,1); // 1==stdout
printf("Second printf is in this file.\n");
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs |
표준 입출력 라이브러리
스트림 열기/닫기
#include <stdio.h>
FILE *fopen(const char *pathname, const char *type);
int fclose (FILE *fp);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#include <stdio.h>
int main()
{
FILE *fp;
printf("Success!\n");
printf("Opening\"test.txt\" int \"r\" mode!\n");
}else{
perror("Error");
return -1;
}
fclose(fp);
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs |
문자열 기반 입출력
문자 단위 입력
#include <stdio.h>
int getc(FILE *fp);
int fgetc(FILE *fp);
int getchar(void);
지정된 파일에서 한 문자를 읽어오는 함수
성공하면 읽은 문자, 실패하거나 파일의 끝이면 EOF을 반환
getc()는 매크로이고 fgetc()는 함수로 구현
#define getchar() getc(stdin)
int ungetc(int c, FILE *fp);
지정된 문자를 스트림의 맨 앞으로 반환
성공하면 c, 실패하면 EOF을 반환
특정 조건을 검사하기 위해 하나의 문자를 읽은 후, 원래의 상태로 복원하기 위해 이용
문자 단위 출력
#include <stdio.h>
int putc(int c, FILE *fp);
int futc(int c, FILE *fp);
int putchar(int c, FILE *fp);
지정된 파일에 한 문자를 쓰는 함수
성공하면 c, 실패하면 EOF을 반환
putc()는 매크로, fputc()는 함수로 구현
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
|
#include <stdio.h>
int main()
{
int c,number=0;
char op;
FILE *fp;
perror("File open error");
return -1;
}
while(!feof(fp)){
while((c=fgetc(fp))!=EOF && isdigit(c))
number=10*number+c-'0';
if(c=='\n') continue;
fprintf(stdout,"Operand=>%d\n",number);
number=0;
if(c!=EOF){
ungetc(c,fp);
op=fgetc(fp);
fprintf(stdout,"Operator=>%c\n",op);
}
}
fclose(fp);
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs |
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
|
#include <stdio.h>
int main(int argc,char *argv[])
{
int c;
FILE *in, *out;
if(argc!=3){
fprintf(stderr,"Usage: append_char file1 file2\n");
return -1;
}
if((in=fopen(argv[1],"r"))==NULL){
perror(argv[1]);
return -1;
}
if((out==fopen(argv[2],"a"))==NULL){
perror(argv[2]);
return -1;
}
while((c==getc(in))!=EOF) putc(c,out);
fclose(out);
fclose(in);
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs |
'딥러닝 기반 영상인식 개발 전문가 과정 > 리눅스' 카테고리의 다른 글
5월29일 실습 (0) | 2019.05.29 |
---|---|
5월29일 리눅스 2 형기반입출력, 줄단위, 버퍼기반, 읽기/쓰기 오프셋 재배치, 디렉토리 생성과 삭제 (0) | 2019.05.29 |
5월28일 유틸리티 루틴, 저수준 입출력 루틴 (0) | 2019.05.28 |
5월 27일 VIM (0) | 2019.05.27 |
5월25일 리눅스 실습들 (0) | 2019.05.25 |