fclose
From cppreference.net
File input/output
| Types and objects | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
헤더 파일에 정의됨
<stdio.h>
|
||
|
int
fclose
(
FILE
*
stream
)
;
|
||
주어진 파일 스트림을 닫습니다. 기록되지 않은 버퍼링된 데이터는 OS로 플러시됩니다. 읽히지 않은 버퍼링된 데이터는 폐기됩니다.
작업의 성공 여부와 관계없이, 스트림은 더 이상 파일과 연결되지 않으며, setbuf 또는 setvbuf 에 의해 할당된 버퍼(존재하는 경우)도 연결이 해제되고, 자동 할당이 사용된 경우 할당이 해제됩니다.
포인터
stream
의 값이
fclose
가 반환된 후에 사용되면 동작은 정의되지 않습니다.
목차 |
매개변수
| stream | - | 닫을 파일 스트림 |
반환값
0 성공 시, EOF 그렇지 않으면
예제
이 코드 실행
#include <stdio.h> #include <stdlib.h> int main(void) { const char* fname = "/tmp/unique_name.txt"; // or tmpnam(NULL); int is_ok = EXIT_FAILURE; FILE* fp = fopen(fname, "w+"); if (!fp) { perror("File opening failed"); return is_ok; } fputs("Hello, world!\n", fp); rewind(fp); int c; // note: int, not char, required to handle EOF while ((c = fgetc(fp)) != EOF) // standard C I/O file reading loop putchar(c); if (ferror(fp)) puts("I/O error when reading"); else if (feof(fp)) { puts("End of file is reached successfully"); is_ok = EXIT_SUCCESS; } fclose(fp); remove(fname); return is_ok; }
가능한 출력:
Hello, world! End of file is reached successfully
참조문헌
- C23 표준 (ISO/IEC 9899:2024):
-
- 7.21.5.1 fclose 함수 (p: TBD)
- C17 표준 (ISO/IEC 9899:2018):
-
- 7.21.5.1 fclose 함수 (p: TBD)
- C11 표준 (ISO/IEC 9899:2011):
-
- 7.21.5.1 fclose 함수 (p: 304)
- C99 표준 (ISO/IEC 9899:1999):
-
- 7.19.5.1 fclose 함수 (p: 270)
- C89/C90 표준 (ISO/IEC 9899:1990):
-
- 4.9.5.1 fclose 함수
참고 항목
|
(C11)
|
파일 열기
(함수) |
|
(C11)
|
기존 스트림을 다른 이름으로 열기
(함수) |
|
C++ documentation
for
fclose
|
|