파일입출력 모드 아시는 분

corone의 이미지

파일입출력 모드 중 r+로 열었을 때
파일이 존재하지 않으면
1) 새로 생성하는지
2) NULL을 리턴하는지
질문드립니다.

제가 실제로 해본 결과는
MS VC++에서 2) NULL을 리턴합니다
gcc에서도 2) NULL을 리턴했습니다

이에 대해 표준 있으면 알려주시면 좋겠고
컴파일러나 버전 차이가 있으면 알려주시면 진심으로 감사하겠습니다.

cdecl의 이미지

MSDN 에 있는 내용입니다.

"r" 이나 "r+" 는 파일을 생성하지는 않습니다.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_fopen.2c_._wfopen.asp

The character string mode specifies the type of access requested for the file, as follows:

"r"
Opens for reading. If the file does not exist or cannot be found, the fopen call fails.
"w"
Opens an empty file for writing. If the given file exists, its contents are destroyed.
"a"
Opens for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; creates the file first if it doesn't exist.
"r+"
Opens for both reading and writing. (The file must exist.)
"w+"
Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed.
"a+"
Opens for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it doesn't exist.

--
cdecl

corone의 이미지

명쾌한 답변을 주시긴 했지만
생각해 보니까 MSDN이 C언어의 표준은 아니지 않습니까?
가끔 MS VC++와 gcc가 다른 결과를 내기도 하던데
gcc가 표준이라고 생각하는데

물고늘어지는 것 같아 죄송합니다...

cinsk의 이미지

ISO C 표준에 따라, "r+"를 썼다면 fopen(3)은 파일이 없을 경우, 새로 만들지 않습니다.. 따라서 새로 만드는 C library가 있다면, 표준에 부합하는 라이브러리가 아닙니다.

--
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Korean Ver: http://www.cinsk.org/cfaqs/

corone의 이미지

두분 모두 제가 원하는 답변을 주셨습니다.
두분 모두에게 진심으로 감사드립니다.