반응형
#include <stdio.h>
int fputs(const char* str, FILE* stream);
첫 번째 인자로 파일에 저장할 문자열
두 번째 인자 스트림 파일 포인터
리턴 값 : 쓰기를 성공했다면 음이 아닌 수가 리턴된다. 오류가 발생한다면 EOF 를 리턴한다.
#include <stdio.h>
int main()
{
FILE* pFile = NULL;
// w(쓰기모드) t(텍스트모드)
fopen_s(&pFile, "sample.txt", "wt");
if (pFile)
{
const char* str1 = "0123456789\n";
const char* str2 = "abcdefghijklmnopqrstuvwxyz\n";
fputs(str1, pFile);
fputs(str2, pFile);
fclose(pFile);
}
return 0;
}
// sample.txt 확인
// 0123456789
// abcdefghijklmnopqrstuvwxyz
만약 fopen_s 에서 "wt" 가 아닌 "at" 로 하면 추가모드로 되기 때문에
기존의 sample.txt 내용이 지워지지 않고, 추가가 된다.
반응형