fopen과, fclose, fwrite에 관해서 질문.

aninly의 이미지

static inline void save_yuv(int width, int height, int bpp, int iter)
{
 	FILE *yuv_fp = NULL;
	char file_name[100];
	
	 /* read from device */
	if (read(cam_fp, g_yuv, width*height*bpp/8) < 0) {
		perror("read()");
	}

	if (bpp == 16 ) {
		sprintf(&file_name[0], "422X%d.yuv", iter);
		printf("422X%d.yuv", iter);
	}
	else { 
		sprintf(&file_name[0], "420X%d.yuv", iter);
		printf("420X%d.yuv\n", iter);
	}
	fflush(stdout);
	/* file create/open, note to "wb" */
	yuv_fp = fopen(&file_name[0], "wb");
	if (!yuv_fp) {
		perror(&file_name[0]);
	}
	fwrite(g_yuv, 1, width * height * bpp / 8, yuv_fp);
	fclose(yuv_fp);
}
    1. 위의 함수에서 yuv_fp=fopen(&file_name[0], "wb");에 대해서 알고 싶습니다.
    &file_name[0]부분에 "abc"이런식으로 들어가는걸로 알고 있는데 이런식으로 되어있는지.
    "wb"는 무슨 뜻인지 ''r"은 읽기라는건 아는데 말이죠.

    2. fwrite(g_yuv, 1, width * height * bpp / 8, yuv_fp);
    에서 어디에 무엇을 쓰는지 모르겠습니다.
    g_yuv에 yuv_fp를 저장하는건지. 반대로 인지
    그리고 1은 무슨 의미인지 알고 싶습니다.

[/]
vananamilk의 이미지

aninly wrote:

1. 위의 함수에서 yuv_fp=fopen(&file_name[0], "wb");에 대해서 알고 싶습니다.
&file_name[0]부분에 "abc"이런식으로 들어가는걸로 알고 있는데 이런식으로 되어있는지.
"wb"는 무슨 뜻인지 ''r"은 읽기라는건 아는데 말이죠.

file_name은 스트링 배열입니다. 간단히 말해 배열은 시작주소죠.
file_name에 "abc.txt"가 저장되어 있다구 합시당.
file_name은 배열이름이니 주소구요. 0xffffxxx 이런식의 주소구요.
&file_name[0]두 같은 의미에요. file_name[0]은 'a'이구요. 이 곳의
주소를 나타내는 &를 기호를 사용했으니 0xffffxxx가 되겠죠... 고로
같은 의미죠. 보통 file_name 이렇게 사용해요.
"wb"는 file에 write 할 수 있다는거죠. b는 바이너리인뎅. windows 환경에서
사용하죠. text 파일이 아닌 binary 파일 읽을때 사용하는걸로 알구 있구요.
unix, linux에서는 바이너리, 텍스트의 구분이 없는걸로 알고 있습니다.

aninly wrote:

2. fwrite(g_yuv, 1, width * height * bpp / 8, yuv_fp);
에서 어디에 무엇을 쓰는지 모르겠습니다.
g_yuv에 yuv_fp를 저장하는건지. 반대로 인지
그리고 1은 무슨 의미인지 알고 싶습니다.

size_t fwrite( const void *ptr, size_t size, size_t nmemb, FILE
*stream);

fwrite는 open된 파일에 write 한다는 뜻이구요. yuv_fp로 open된 파일에
1 * (width * height * bpp / 8) 크기만큼을 write한다는겁니다.
g_yuv는 아마 배열이겠죠... g가 붙은걸 보니 전역변수군요.

1 하구 (width * height * bpp / 8)가 서로 바뀌어야 될것 같은데요. 리눅스
에서는 앞에께 크기구 뒤에것이 갯수거든요.
16byte크기를 1개 쓴다는거... 바껴두 상관은 없죠. 어차피 같은뎅~

카메라 같은 장치에서 읽어와서 파일에 저장하는 함수 같네요^

그럼.

mirr의 이미지

1. wb는 바이너리형식으로 쓰겠다는 걸 나타냅니다.
파일기록방법에는 텍스트모드와 바이너리모드가 있습니다.

2. 라이브러리 함수 fwrite()는 메모리의 데이터를 블록 단위로 이진 모드의 파일에 기록합니다.
STDIO.H에 정의되어 있는 함수의 원형은 다음과 같습니다.

int fwrite(void *buf, int size, int count, FILE *fp);

인수 buf는 파일에 기록할 데이터가 저장되어 있는 메모리 영역에 대한 포인터이고.
포인터의 형은 void이므로 어떤 데이터형에 대한 포인터가 될 수 있ㅅ습니다.
인수 size는 개별적인 데이터 항목의 크기를 바이트 단위로 지정하는 것이고, count는 기록할 항목의 수를 지정합니다.

예를 들어, 100개의 요소를 가지는 정수형 배열을 저장하기 원한다면
각각의 int형은 2바이트를 차지하므로 size는 2가 될 것이고, 배열은 100개의 요소를 가지므로 count는 100이 됩니다.

size 인수를 계산하기 위해서 sizeof() 연산자를 사용할 수 있고,
인수 fp는 물론 파일을 열 때 fopen()이 돌려주는 FILE형에 대한 포인터입니다.

즉 위에서 질문하신 내용을 설명하자면
yuv_fp로 열려진 파일에 1만큼의 크기를갖는내용을(char형인듯 하군요.)
width * height * bpp/8만큼 기록하겠다는 것입니다. yuv_fp는
파일포인터구요.

쓰는동안 답변달렸군요.....

내 마음속의 악마가 자꾸만 나를 부추겨.
늘 해왔던 것에 만족하지 말고 뭔가 불가능해 보이는 것을 하라고 말야.

chadr의 이미지

첨언 하자면 바이너리 모드와 텍스트 모드를 사용하실 때 주의사항이 있습니다.

Quote:

t

Open in text (translated) mode. In this mode, CTRL+Z is interpreted as an end-of-file character on input. In files opened for reading/writing with "a+", fopen checks for a CTRL+Z at the end of the file and removes it, if possible. This is done because using fseek and ftell to move within a file that ends with a CTRL+Z, may cause fseek to behave improperly near the end of the file.
Also, in text mode, carriage return–linefeed combinations are translated into single linefeeds on input, and linefeed characters are translated to carriage return–linefeed combinations on output. When a Unicode stream-I/O function operates in text mode (the default), the source or destination stream is assumed to be a sequence of multibyte characters. Therefore, the Unicode stream-input functions convert multibyte characters to wide characters (as if by a call to the mbtowc function). For the same reason, the Unicode stream-output functions convert wide characters to multibyte characters (as if by a call to the wctomb function).

위 글의 출처는 msdn입니다.

참고 하시길 바랍니다.

-------------------------------------------------------------------------------
It's better to appear stupid and ask question than to be silent and remain stupid.

댓글 달기

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