[C] C의 파일시스템, fopen_s()

2025. 7. 24. 20:20·소프트웨어/C

C에서 파일의 데이터를 읽거나 쓸 경우 파일 입출력 함수인 fopen_s()를 사용하게 된다.

 

fopen_s의 구문은 다음과 같다.

errno_t fopen_s(
   FILE** pFile,
   const char *filename,
   const char *mode
);

매개변수

pFile : 열린 파일에 대한 포인터를 받는 파일 포인터에 대한 포인터이다.

filename : 열어야 할 파일의 이름이다.

mode : 허용되는 엑세스 형식이다.

반환 값

성공 시 0이고, 실패 시 오류 코드를 반환한다.

 

예시 코드

// crt_fopen_s.c
// This program opens two files. It uses
// fclose to close the first file and
// _fcloseall to close all remaining files.

#include <stdio.h>

FILE *stream, *stream2;

int main( void )
{
   errno_t err;

   // Open for read (will fail if file "crt_fopen_s.c" doesn't exist)
   err  = fopen_s( &stream, "crt_fopen_s.c", "r" );
   if( err == 0 )
   {
      printf( "The file 'crt_fopen_s.c' was opened\n" );
   }
   else
   {
      printf( "The file 'crt_fopen_s.c' was not opened\n" );
   }

   // Open for write
   err = fopen_s( &stream2, "data2", "w+, ccs=UTF-8" );
   if( err == 0 )
   {
      printf( "The file 'data2' was opened\n" );
   }
   else
   {
      printf( "The file 'data2' was not opened\n" );
   }

   // Close stream if it isn't NULL
   if( stream )
   {
      err = fclose( stream );
      if ( err == 0 )
      {
         printf( "The file 'crt_fopen_s.c' was closed\n" );
      }
      else
      {
         printf( "The file 'crt_fopen_s.c' was not closed\n" );
      }
   }

   // All other files are closed:
   int numclosed = _fcloseall( );
   printf( "Number of files closed by _fcloseall: %u\n", numclosed );
}

출력

The file 'crt_fopen_s.c' was opened
The file 'data2' was opened
Number of files closed by _fcloseall: 1

 

 

 

refernece https://learn.microsoft.com/ko-kr/cpp/c-runtime-library/reference/fopen-s-wfopen-s?view=msvc-170

'소프트웨어 > C' 카테고리의 다른 글

[C] 헤더파일에 대하여  (0) 2025.08.02
[C] C언어의 extern 키워드  (0) 2025.07.30
[C] C언어의 조건부 컴파일 지시자(#if #ifdef #ifndef #endif)  (0) 2025.07.17
[C] volatile 키워드  (0) 2025.06.23
[C/C++] #include "ex.h" 과 <ex.h>의 차이  (0) 2025.03.26
'소프트웨어/C' 카테고리의 다른 글
  • [C] 헤더파일에 대하여
  • [C] C언어의 extern 키워드
  • [C] C언어의 조건부 컴파일 지시자(#if #ifdef #ifndef #endif)
  • [C] volatile 키워드
jh-rrr
jh-rrr
기술의 깊이에 집중하며 성장하길 지향합니다.
  • jh-rrr
    Embedded World
    jh-rrr
  • 전체
    오늘
    어제
    • 분류 전체보기 (64)
      • 소프트웨어 (17)
        • 프로그래밍 (2)
        • C (10)
        • Python (1)
        • 운영체제 (3)
        • 네트워크 (0)
      • Embedded Systems (16)
        • 리눅스 (10)
        • MCU 기본 (2)
        • 임베디드 레시피 (0)
      • Projects (1)
        • Cortex-M3 (1)
        • 재난 구조 로봇 (0)
      • AI (11)
        • Computer Vision (2)
        • Deep Learning (3)
        • cs224n (2)
        • cs231n (2)
      • 취업 준비 (0)
        • 프로젝트 & 자격증 (1)
      • 엔지니어링 뉴스 (3)
      • Paper Reviews (4)
      • Insights (8)
        • Seminar ! (2)
        • 서평 (4)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    리눅스 오류
    kernel image 란
    OS 이미지
    리눅스
    conda: command not found
    커널 이미지
    커널 이미지란
    essential deep learning paper reading
    stm32f 시리즈를 이용한 arm cortex-m3/m4 구조와 응용
    일귀
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.5
jh-rrr
[C] C의 파일시스템, fopen_s()
상단으로

티스토리툴바