본문 바로가기

지식/system

fwrite 가 1을 리턴할때..

MSDN 의 fwrite 설명 









size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream );

Parameters

buffer

Pointer to data to be written.

size

Item size in bytes.

count

Maximum number of items to be written.

stream

Pointer to FILE structure.

Return Value

fwrite returns the number of full items actually written, which may be less than count if an error occurs. Also, if an error occurs, the file-position indicator cannot be determined. If either stream or buffer is a null pointer, the function invokes the invalid parameter handler, as described in Parameter Validation. If execution is allowed to continue, this function sets errno to EINVAL and returns 0.

Remarks

The fwrite function writes up to count items, of size length each, from buffer to the output stream. The file pointer associated with stream (if there is one) is incremented by the number of bytes actually written. If stream is opened in text mode, each carriage


보면 size 인 데이터를 count 갯수만큼 기록하게되어있다.
가령 구조체를 예를 들자면

typedef struct{
   int a;
   int b;
   char c;
}TEST_T;

TEST_T t_data;
int data_size = sizeof(TEST_T);
FILE *fp = fopen("test.data","wb");

여기서 파일을 쓸 때 보통같으면

iwrite = fwrite(t_data,data_size,1,fp);

일것이다. 하지만 황당하게 iwrite 값이 1인 경우가 나왔다..
버그인듯..

그래서 아래와같이
iwrite = fwrite(t_data,1,data_size,fp);

size 와 count 인자를 바꿔주니 값이 제대로 나왔다...
참고하자.

테스트환경 : WinXP + VS2005 SP1