리눅스 질문

익명 사용자의 이미지

fdopen과 lseek를 활용해서 fseek를 구현하려 합니다.
임의로 파일을 생성하고 제가 구현한 프로그램을 돌리고 난 후 ls -l로 파일 사이즈를 확인하는 방식으로 확인하려 했습니다.
파일을 생성하고 Linux라고 입력을 했고 사이즈가 6byte가 나온걸 확인했고
이후에 제 프로그램을 실행시켜 30byte만큼 fseek 하게 하려고 했고 그럼 36byte가 나와야하는데 여전히 6byte가 나오더군요 뭐가 문제일까요

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
 
int main(int argc, char *argv[])
{
        FILE *fp;
        int fd;
        int offset;
 
        printf("Enter Jumping Bytes : ");
        scanf("%d",&offset);
        if(argc != 3)
        {
                perror("Parameter Error!\n");
                exit(-1);
        }
        else
        {
                if((fd = open(argv[1], O_RDONLY, 0644)) == -1)
                {
                        perror("File Open Error!\n");
                        exit(-1);
                }
 
                if((fp = fdopen(fd, "r")) == NULL)
                {
                        perror("Stream Creating Error!\n");
                        exit(1);
                }
 
                if(strcmp(argv[2], "SEEK_SET") == 0)
                {
                        if(lseek(fd, offset, SEEK_SET) == -1)
                        {
                                perror("lseek Error!\n");
                                exit(1);
                        }
                }
                else if(strcmp(argv[2], "SEEK_CUR") == 0)
                {
                        if(lseek(fd, offset, SEEK_CUR) == -1)
                        {
                                perror("lseek Error!\n");
                                exit(1);
                        }
                }
                else if(strcmp(argv[2], "SEEK_END") == 0)
                {
                        if(lseek(fd, offset, SEEK_END) == -1)
                        {
                                perror("lseek Error!\n");
                                exit(1);
                        }
                }
        }
        fclose(fp);
        return 0;
}

File attachments: 
첨부파일 크기
Image icon 실행 전 파일상태17.49 KB
Image icon 실행 후17.05 KB
chanik의 이미지

https://man7.org/linux/man-pages/man2/lseek.2.html

       lseek() allows the file offset to be set beyond the end of the
       file (but this does not change the size of the file).  If data is
       later written at this point, subsequent reads of the data in the
       gap (a "hole") return null bytes ('\0') until data is actually
       written into the gap.

lseek() 매뉴얼 페이지에 나오길, 파일 끝 너머로 lseek() 할 수는 있으나 그 위치에 쓰기가 이뤄지기 전까지는 lseek()만으로 파일크기가 바뀌지는 않는다는군요. 그리고, 지금은 파일을 읽기전용으로 열고 있기도 하네요.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.