-
함수 vsnprintf()검색하기 귀찮아서 블로그에 박제 2021. 4. 28. 15:06728x90
#include <stdio.h>
int snprintf(char *str, size_t size, const char *format, ...);
#include <stdarg.h>
int vsnprintf(char *str, size_t size, const char *format, va_list ap);이 함수들을 사용하는 이유 = 주어진 size만큼만 str에 print하기 위함. 즉, buffer overflow를 막기 위함
// crt_vsnprintf.c // compile by using: cl /W4 crt_vsnprintf.c #include <stdio.h> #include <stdarg.h> // for va_list, va_start #include <string.h> // for memset #define BUFFCOUNT (10) void FormatOutput(char* formatstring, ...) { int nSize = 0; char buff[BUFFCOUNT]; memset(buff, 0, sizeof(buff)); va_list args; va_start(args, formatstring); nSize = vsnprintf(buff, sizeof(buff), formatstring, args); printf("nSize: %d, buff: %s\n", nSize, buff); va_end(args); } int main() { FormatOutput("%s %s", "Hi", "there"); // 8 chars + null FormatOutput("%s %s", "Hi", "there!"); // 9 chars + null FormatOutput("%s %s", "Hi", "there!!"); // 10 chars + null } ********************************************************************* output: nSize: 8, buff: Hi there nSize: 9, buff: Hi there! nSize: 10, buff: Hi there!
728x90'검색하기 귀찮아서 블로그에 박제' 카테고리의 다른 글
[PyTorch error] BrokenPipeError: [Errno 32] Broken pipe 해결 (0) 2021.07.19 "Program received signal SIGPIPE, Broken pipe." 해결방법 (0) 2021.04.29 ModuleNotFoundError : from tensorflow.compat.v1 import * (0) 2021.04.21 pytorch yolov3 (0) 2021.02.03 docker 환경 구축하기 (0) 2021.02.03