Fread
fread 함수는 스트림에서 바이너리 데이터를 읽어 버퍼에 저장하는 표준 C 라이브러리 함수이다.
정의
<stdio.h> 헤더 파일에 다음과 같이 정의되어 있다.
size_t fread (void * DstBuf, size_t ElementSize, size_t Count, FILE * FileStream)
인수
각 인수는 다음과 같은 의미를 가지고 있다.
DstBuf- 입력받은 데이터를 저장할 버퍼의 주소
ElementSize- 원소 1개의 크기
Count- 입력 받을 원소의 개수
FileStream- 파일 스트림
반환값
실제로 읽어들인 원소의 개수를 반환한다.
반환값이 Count보다 작으면 오류나 파일 끝(EOF)의 경우이다.
동작
파일 스트림으로부터 ElementSize크기의 바이너리 데이터를 Count회 읽어 DstBuf에 저장한다. Count회 입력이 완료되거나 EOF를 입력받았을 때, 혹은 오류가 발생했을 때 입력을 마친다. 함수가 종료될 때 입력이 완료된 곳까지 파일 포인터를 이동시키고 실제로 읽어들인 원소의 개수를 반환한다.
예제
#include <stdio.h>
int main(void)
{
FILE *file_ptr;
char arr[6];
size_t iCount;
file_ptr = fopen("sample.txt", "rb");
if(file_ptr==NULL) return 1;
iCount = fread(arr, sizeof(char), sizeof(arr), file_ptr);
fclose(file_ptr);
return 0;
}
위 예제는 sample.txt에서 6바이트를 읽어(파일이 6바이트보다 크다는 가정 하에) arr에 저장하고 읽은 바이트 수를 iCount에 저장한다.
같이 보기
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.