안녕하세요. 파일 offset관련 질문입니다.

bluesaiha의 이미지

저수준의 FILE I/O를 이용해서 고수준의 FILE I/O를 구현하는 것이 목적인데요

파일 디스크립터와 파일 포인터를 쓴다는 두 I/O간의 차이를 알아내고 fileno()함수를 통해서 파일 포인터를 파일디스크립터로 변환하여 함수내에서 저수준의 FILE I/O 함수를 사용해서 같은 기능을 구현하게 하려는데요

제가 마주친 문제는
fseek() 함수를 구현하는데 lseek()를 사용해서 구현하려고 합니다.

int myFseek(FILE *fp, long offset, int whence){
    off_t loc;
    int fd;
    int tmp_offset;
 
    fd = fileno(fp);    
    loc = lseek(fd, offset, whence);    
    printf("loc : %d\n",(int)loc);
    if(loc == -1){
	perror("Offset Error");
	return -1;
    }
    else 
	return 0;     
}

위와 같이 구현을 해보았는데요. 문제점이 loc = lseek(fd, offset, whence) 부분에서
loc의 값이 현재 offset값이 나와야하는데 자꾸 파일의 전체 바이트 값이 나옵니다.ㅠ
무엇이 잘 못 짜여졌는지좀 알려주세요.(3시간째 어떻게 해결해야하는지 몰라서 헤매고 있습니다.ㅠ)

myFopen과 myFclose는 제대로 작성이 되었는지도 궁금합니다.ㅠ

혹시 몰라 전체 작성중이던 소스를 첨부합니다

#include <stdlib.h>
#include <stdio.h>
 
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
 
#define BUFFSIZ 1024
 
FILE *myFopen(char name[], char *mode){
    int fd;
    FILE *fp;    
 
    fd = open(name, *mode);
    if(fd==-1) {
    perror("Open error");
    exit(1);
    }
 
    fp = fdopen( fd, mode );
    return fp;
}
 
FILE *myFclose(FILE *fp){
    int fd;
 
    fd = fileno(fp);
    close(fd);
    return (FILE *)fd;
}
 
int myFseek(FILE *fp, long offset, int whence){
    off_t loc;
    int fd;
    int tmp_offset;
 
    tmp_offset = ftell(fp);
    fd = fileno(fp);
    lseek(fd,tmp_offset,SEEK_SET);
    loc = lseek(fd, offset, SEEK_CUR);    
    printf("loc : %d\n",(int)loc);
    if(loc == -1){
	perror("Offset Error");
	return -1;
    }
    else 
	return 0;     
}
 
int myFtell(FILE *fp){
    int off_set;
    int fd;
    fd = fileno(fp);
    off_set = lseek(fd, 0, SEEK_CUR);
 
    return off_set;
}
 
int myFsetpos(FILE *fp, const fpos_t *pos){
    int fd;
 
    fd = fileno(fp);
 
 
    return 0;
}
 
int myFgetpos(FILE *fp, fpos_t *pos){
 
}
 
int main(int argc, char*argv[]) {
    FILE *fp;
    int n;
    long cur;
    char buf[BUFFSIZ];
 
    if ((fp = myFopen(argv[1], "r")) == NULL) {
        perror("myFopen");
        exit(1);
    }
 
    cur = myFtell(fp);
    printf("Offset cur=%d\n", (int)cur);
 
    n = fread(buf, sizeof(char), 4, fp);
    buf[n] = '\0';
    printf("-- Read Str=%s\n", buf);
 
    myFseek(fp, 1, SEEK_CUR);
 
    cur = myFtell(fp);
    printf("Offset cur=%d\n", (int)cur);
 
    n = fread(buf, sizeof(char), 6, fp);
    buf[n] = '\0';
    printf("-- Read Str=%s\n", buf);
 
    cur = 12;
 
    fsetpos(fp, &cur);
 
    fgetpos(fp, &cur);
    printf("Offset cur=%d\n", (int)cur);
 
    n = fread(buf, sizeof(char), 13, fp);
    buf[n] = '\0';
    printf("-- Read Str=%s\n", buf);
 
    myFclose(fp);
    printf("Check closed fp %p\n", fp);
 
    return 0;
}

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.