Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- c
- 브런치
- QA
- IT
- For Beginners
- 5622
- Zero That Out
- file IO
- File 조작
- 균형잡힌 세상
- Parenthesis
- 백준
- 전자책
- 입력 버퍼
- 2941
- 10773
- c++
- system programming
- 바샤
- BAKA
- 시프
- 4949
- 1874
- LJESNJAK
- Baekjoon
- 시스템 프로그래밍
- process control
- The Balance of the World
- 해바
- Process Communication
Archives
- Today
- Total
해바
Lab #2 본문
lseek() 시스템 호출을 사용하여, 파일의 원하는 부분을 출력하도록 만든다 :
- 테스트용 입력파일 "test.txt"에 "abcdefghijklmnop"를 저장한다.
- lseek 를 사용하여 파일의 시작에서 10개의 character를 건너뛴 후 부터의 내용을 출력한다.
- lseek 를 사용하여 파일의 끝에서 앞쪽으로 5개의 character를 건너뛴 후 그 내용을 출력한다.
- 자기 홈 디렉토리에 lab2 디렉토리를 만든 후 프로그램 파일 size.c를 보관할 것
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <error.h>
#include <stdio.h>
#define BUFSIZE 512
int main() {
int fd;
char buf1[BUFSIZE] = "";
char buf2[BUFSIZE] = "";
if ((fd = open("test.txt", O_RDONLY)) == -1) {
perror("File cannot be opened.");
}
if(lseek(fd, 10, SEEK_SET) == -1) // 파일 시작에서 10개를 건너뛴 후 내용들 출력
perror("lseek 1");
read(fd, buf1, BUFSIZE);
printf("%s\n", buf1);
if(lseek(fd, -5, SEEK_END) == -1) // 파일 끝에서 5개를 건너뛴 후 그 내용을 출력
perror("lseek 2");
read(fd, buf2, BUFSIZE);
printf("%s\n", buf2);
close(fd);
return 0;
}
컴파일 :
gcc size.c
실행 :
./a.out
Comments