Guest User

read()

a guest
May 19th, 2016
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.57 KB | None | 0 0
  1.  
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<fcntl.h>
  5. int main()
  6. {
  7.     int fd;
  8.     fd = open("test.txt", O_RDONLY);        // 파일을 읽기전용으로 오픈하기
  9.  
  10.     char * array = (char * )malloc(1024);  
  11.     memset(array, 0, 1024);                    // array를 0으로 초기화
  12.  
  13.     read(fd, array, 1024);    
  14.     // 파일디스크립터를 통해 접근하여 파일 내용을 array에 파일의 크기만큼 저장
  15.  
  16.     printf("%s\n", array);                       // 파일의 내용출력
  17.  
  18.     close(fd);
  19.     free(array);
  20.  
  21.     return 0;
  22. }
Add Comment
Please, Sign In to add comment