파일크기 - 해당되는 글 1건

파일 크기 바꾸기

truncate, ftruncate 함수
기능
       파일을 지정한 크기로 자른다.
기본형
       int truncate(const char *path, off_t length);
       int ftruncate(int fd, off_t length);
       path : 자르고자 하는 파일의 이름
       fd : 자르고자 하는 파일의 파일 식별자
       length : 파일의 크기
반환값
       성공 : 0
       실패 : -1
헤더 파일
       <unistd.h>

--------------------------------------

#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>

main(int argc, char *argv[])
{
    int fd;

    if ((fd = open(argv[1], O_WRONLY)) == -1) {
       perror("open failed");
       exit(1);
    }
    /* fd 파일을 150 바이트 크기로 만든다. 파일의 크기가 150바이트보다
     작아도 150바이트 크기가 되는데 빈 공간은 NULL로 채워진다. */
    if (ftruncate(fd, 150) == -1) {
       perror("ftruncate failed");
       exit(1);
    }
    close(fd);
    exit(0);
}
----------------------------------

      c 언어  |  2007. 9. 13. 12:43



sokoban's Blog is powered by Daum & Tattertools