[완료]파일 디스크립터와 파일 포인터 사용시 의문점.

pchero의 이미지

네트워크 프로그래밍을 공부하다가 의문이 떠올랐습니다.

순전히 호기심에 다음과 같은 코드를 실행해 보았는데 조금 이상한 결과가 나왔습니다.

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
 
void error_handling(char *message);
 
int main(void)
{
	int fildes;
	FILE *fp;
 
	fildes = open("data.dat", O_WRONLY | O_CREAT | O_TRUNC);
	if(fildes == -1)
		error_handling("open() error!");
 
	/* 파일 디스크립터를 이용하여 파일 포인터 생성 */
	fp = fdopen(fildes, "w");
	write(fileno(fp), "This is write funcion1!! \n\n", 27);
	fputs("This is fputs function!! \n\n", fp);
	write(fileno(fp), "This is write funcion2!! \n\n", 27);
 
	fclose(fp);
 
	return 0;
}
 
void error_handling(char *message)
{
	fputs(message, stderr);
	fputc('\n', stderr);
	exit(1);
}

이것은 실행화면 입니다.

여기서 궁금한 것이....

하나의 파일 디스크립터를 파일 포인터로 변환해서 같은 파일에 글을 쓰고 있는데...

결과를 보시면 호출하는 순서에 상관없이 디스크립터를 이용하여 입력한 데이터가 파일의 처음부분에 나오고 포인터를 이용해서 입력한 데이터가 파일의 마지막 부분에 나옵니다.

저는 호출한 순서에 맞게끔 입력이 될줄 알았는데...왜 이런 결과가 나오는 것인가요..?

File attachments: 
첨부파일 크기
Image icon test.png23.54 KB
익명 사용자의 이미지

fp를 통한 read/write는 c library에서 자체적으로 버퍼를 가지고 있을 겁니다.
읽어올때도 요청된 것만큼(이상) 충분히 읽어오고,
적절한 만큼 쌓인 후에야 실제 쓰기를 수행하구요.

pchero의 이미지

감사합니다

	fputs("This is fputs function!! \n\n", fp);
	fflush(fp);

처럼 fputs() 이후에 fflush() 함수를 넣어주니 생각대로 결과값이 잘 나왔습니다

감사합니다 : )

---------------------------------
제일 왼쪽이 저입니다 :)

---------------------------------
제일 왼쪽이 저입니다 :)

익명 사용자의 이미지

매번 fflush() 하는 부담까지 없애려면 setvbuf() 를 사용하면 됩니다.
아예 버퍼링을 하지 않거나, 라인단위로만 버퍼링을 하거나 등등 선택의 여지도 많습니다.

댓글 달기

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