파일 입출력 함수를 만들고 있는데,, 이해가 잘 안갑니다 . ㅠ

clover6652의 이미지

#include
#include
#include
#include
#include
#include
#include

typedef struct{
char* mode;
int file;
char *name;
char buffer[1024];
int posit;
}_FILE;

_FILE *_fopen(char *path, char *mode)
{
int fd;
_FILE *tmp=(_FILE *)0;

if(mode == "w+") { fd = open(path,O_CREAT|O_RDWR, 0644); }
else { fd = open(path,O_CREAT|O_RDWR|O_APPEND, 0644); }

if(fd<0) { return (_FILE *)0; }
tmp=malloc(sizeof(_FILE));
memset(tmp, 0, sizeof(_FILE));

tmp->mode = strdup(mode);
printf("file open [%s] [%d]\n",path, fd);
tmp->name = strdup(path);
tmp->file = fd;
tmp->posit = 0;
return tmp;
}

int _fclose(_FILE* stream)
{
int fd = -1;
if(!stream) { return -1; }

fd = stream->file;

printf("file close [%s] [%d]\n",stream->name, fd);

if(fd>=0) { close(fd); }
if(stream->name) { free(stream->name); }
if(stream->mode) { free(stream->mode); }
free(stream);

return 0;
}

size_t _fread(void *ptr,size_t size,size_t nmemb, _FILE *stream)
{
char *p = (char*) ptr;
int file_size = size * nmemb;
read(stream->file,p,file_size);
printf("%s \n",p); //값이 잘 read됐나 p를 출력해 봤는데 이상한 값이 나옵니다.
return 0;
}

size_t _fwrite(const void *ptr,size_t size, size_t nmemb,_FILE *stream)
{
char *p = (char*) ptr;
int file_size = size * nmemb;
int i = stream->posit;
int j = 0;
int remain = 1024 - (stream->posit);

while(file_size == 0){
if(stream->posit > 1023)
{
_fflush();
i = 0;
stream->posit = 0;
}
stream->buffer[i] = p[j];
i++;
j++;
file_size--;
}
}

int _fflush(_FILE* fp)
{
write(fp->file,fp->buffer,1024);

}

int main()
{
_FILE* fp1;
_FILE* fp2;
char* buf;

fp1 = _fopen("a.txt","w+");
if(!fp1)
{
printf("File Open Error a.txt\n");
return -1;
}
fp2 = _fopen("b.txt","a+");
if(!fp1)
{
printf("File Open Error b.txt\n");
_fclose(fp1);
return -1;
}
if(!fp2)
{
printf("File Open Error b.txt\n");
_fclose(fp2);
return -1;
}
printf("File [%s] -> File Descript [%d] \n",fp1->name, fp1->file);
printf("File [%s] -> File Descript [%d] \n",fp2->name, fp2->file);

_fread(buf,sizeof(char),1024,fp1); // a.txt파일을 읽으려고 호출
// buf에 읽은 값이 들어오는 것 같은데 이렇게 호출하면 buf에 읽은 값이 들어가게 되는
// 가는 것이 맞나요?????????????????

printf("File [%s] Read -> %s \n",fp1->name, buf);
_fread(buf,sizeof(char),1024,fp2);
printf("File [%s] Read -> %s \n",fp2->name, buf);
_fwrite(buf,sizeof(char),1024,fp2);
_fread(buf,sizeof(char),1024,fp2);
printf("File [%s} Read -> %s \n",fp2->name, buf);
_fclose(fp1);
_fclose(fp2);
return 0;

}

_fread() 함수를 호출 하면
출력이 이상한 쓰레기 값들만 파일에서 읽어 오네요 ㅠ 이유를 잘 모르겠습니다.
_fwrite() 함수도 파일에 제대로 안써지는데 .. 도와주세요 ㅠ

절차탁마의 이미지

if(mode == "w+") { fd = open(path,O_CREAT|O_RDWR, 0644); }
else { fd = open(path,O_CREAT|O_RDWR|O_APPEND, 0644); }

mode와 "w+"를 비교하면 않되죠.
strcmp(), strncmp() 참조 하시면 됩니다.

clover6652의 이미지

근데 그부분은 고쳤는데 그래도 같은 결과가 나와요 ㅠㅠ

댓글 첨부 파일: 
첨부파일 크기
Image icon 22.JPG0바이트
절차탁마의 이미지

O_APPEND는 파일포인터가 맨끝으로 가지 않나요?

clover6652의 이미지

파일 끝에다가 붙여 넣기 할려구욤 ㅠ

절차탁마의 이미지

그럼 open했을때
파일포인터가 맨끝에 있는데
read를 해서 어쩌자는건지요?

clover6652의 이미지

그르네여 ~!!!!!!!!!!!!! ㅡ0ㅡ;;;;;
그런데 ~~ fp1은 append로 안열었는데 안읽어지드라구여 ;;

댓글 달기

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