그만큼 C fread() 파일 스트림에서 주어진 양의 데이터를 읽는 데 사용되는 표준 라이브러리 함수입니다. 내부에 정의된 fread() 함수는 파일 스트림에서 특정 크기의 주어진 요소 수를 읽고 이를 버퍼 메모리에 저장합니다. fread() 함수가 읽은 총 바이트 수는 읽은 요소 수에 각 요소의 크기(바이트)를 곱한 값입니다.
C fread() 구문
size_t fread (void * buffer , size_t size , size_t count , FILE * stream );>
파일 위치 표시기는 읽은 바이트 수만큼 앞으로 자동으로 이동됩니다. 읽고 있는 객체가 구조나 복잡한 데이터 유형과 같이 쉽게 복사할 수 없는 경우 제대로 작동하지 않습니다.
매개변수
- 버퍼: 읽은 데이터가 저장될 버퍼 메모리 블록에 대한 포인터를 나타냅니다. size: 각 요소의 크기를 바이트 단위로 나타냅니다. count: 읽어올 요소의 개수를 나타냅니다. 스트림: 파일 스트림에 대한 포인터를 나타냅니다.
반환 값
- 이 함수는 파일에서 성공적으로 읽은 요소 수를 반환합니다.
- 반환 값이 개수보다 작으면 오류가 발생했거나 파일 끝에 도달했음을 의미합니다.
- size 또는 count 값이 0이면 fread()는 0을 반환하고 다른 작업을 수행하지 않습니다.
메모: fread() 함수 자체는 파일 끝과 오류를 구별하는 방법을 제공하지 않으며, feof와 ferror를 사용하여 어떤 일이 발생했는지 확인할 수 있습니다.
C fread()의 예
실시예 1
아래 프로그램은 fread() 함수를 보여줍니다.
씨
// C program to illustrate fread() function> #include> int> main()> {> >// File pointer> >FILE>* filePointer;> >// Buffer to store the read data> >char> buffer[100];> >// 'g4g.txt' file is opened in read mode> >filePointer =>fopen>(>'g4g.txt'>,>'r'>);> > >// Data is read from the file into the buffer> >// sizeof(buffer) specifies the size of each element to> >// be read 1 is the number of elements to read> >// filePointer is the file to read from> >while> (!>feof>(filePointer)) {> >fread>(buffer,>sizeof>(buffer), 1, filePointer);> >// Print the read data> >printf>(>'%s'>, buffer);> >}> >fclose>(filePointer);> >return> 0;> }> |
>
누가 학교를 만들었나
>
g4g.txt 파일에 다음 데이터가 포함되어 있다고 가정합니다.
Geeks : DS-ALgo Gfg : DP Contribute : writearticle>
그런 다음 프로그램을 실행한 후 출력은 다음과 같습니다.
Geeks : DS-ALgo Gfg : DP Contribute : writearticle>
실시예 2
이 C 프로그램은 파일의 크기나 개수가 0일 때 fread() 함수의 사용법을 보여줍니다.
씨
// C program to illustrate fread() function> // when size of the file or the value of count is equal to 0> #include> int> main()> {> >// File pointer> >FILE>* filePointer;> >// Buffer to store the read data> >char> buffer[100];> >// 'g4g.txt' file is opened in read mode> >filePointer =>fopen>(>'g4g.txt'>,>'r'>);> >// Case when count is equal to 0> >printf>(>'count = 0, return value = %zu
'>,> >fread>(buffer,>sizeof>(buffer), 0, filePointer));> >// Case when size is equal to 0> >printf>(>'size = 0, return value = %zu
'>,> >fread>(buffer, 0, 1, filePointer));> >return> 0;> }> |
>
자바 튜플
>산출
count = 0, return value = 0 size = 0, return value = 0>
C++
// C++ program to illustrate the vector container> #include> #include> using> namespace> std;> int> main()> {> >// 1d vector with initialization list> >vector<>int>>v1 = { 1, 2, 3, 4, 5 };> >// 2d vector with size and element value initialization> >vectorint>> v2(3, 벡터 |
>
C++의 xor
>산출
v1: 1 2 3 4 5 6 v2: 1 2 3 4 6 v2:- 5 5 5 5 5 5 5 5 5>