document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. //vi print.c
  2.  
  3. #include<stdio.h>
  4. #include<string.h>
  5. #include<stdlib.h>
  6. #include<fcntl.h>
  7. int main()
  8. {
  9.    int fd;
  10.    char Add[7] = " World";    
  11.    // 띄어쓰기(1Byte) + world (5byte) + 문자열 끝의NULL(1Byte) = 7Byte의 공간
  12.  
  13.    fd = open("test.txt", O_RDWR | O_APPEND);    // 읽기&쓰기와 덧붙이기 옵션 추가
  14.    
  15.    write(fd, Add, strlen(Add));    
  16.    // fd가 가리키는 파일의 내용에 Add배열 크기만큼 Add의 내용( World)을 저장
  17.  
  18.    char * array = (char *)malloc( 100 );      // 100byte 크기만큼 할당
  19.    memset(array, 0, 100);                       // 100byte 메모리를 0으로 초기화
  20.  
  21.    fd = open("test.txt", O_RDONLY);         // 읽기 전용으로 파일을 다시열기
  22.    read(fd, array, 100 );           // array에 fd가 가리키는 파일(test.txt)의 내용을 저장
  23.    printf("%s\\n", array);  
  24.    
  25.    close(fd);           // 열어둔 파일 을 닫아두기
  26.    free(array);         // 할당 받은 메모리 반환
  27.  
  28.    return 0;
  29. }
');