IT공부/IT서적

[윤성우 열혈 C프로그래밍] Chapter21

shine94 2025. 1. 30. 00:24

* 스트림(stream) → 한 방향으로 흐르는 데이터 흐름

   프로그램상에서 모니터와 키보드를 대상으로 데이터를 입출력 하기 위해서는 이들을 연결시켜주는 다리가 필요함

   이러한 다리 역할을 하는 매개체를 가리켜 스트림(stream)이라고 함

   ㄴ 콘솔 입출력을 위한 입력 스트림과 출력 스트림은

        프로그램이 실행되면 자동으로 생성되고,

        프로그램이 종료되면 자동으로 소멸되는 스트림이다

 

* 입출력 함수

   문자 출력 함수 : putchar, fputc

   문자 입력 함수 : getchar, fgetc

   문자열 출력 함수 : puts, fputs

   문자열 입력 함수 : gets, fgets

 

* 입출력 이외의 문자열 관련 함수

   문자열의 길이를 반환하는 함수 : strlen

   문자열을 복사하는 함수 : strcpy, strncpy

   문자열을 덧붙이는 함수 : strcat, strncat

   문자열을 비교하는 함수 : strcmp, strncmp

 

* 그외의 변환 함수들

   문자열 → int : atoi

   문자열 → long : atol

   문자열 → double : atof

 

#include <stdio.h>

int main(void)
{
00DE17A0  push        ebp  
00DE17A1  mov         ebp,esp  
00DE17A3  sub         esp,0D8h  
00DE17A9  push        ebx  
00DE17AA  push        esi  
00DE17AB  push        edi  
00DE17AC  lea         edi,[ebp-18h]  
00DE17AF  mov         ecx,6  
00DE17B4  mov         eax,0CCCCCCCCh  
00DE17B9  rep stos    dword ptr es:[edi]  
00DE17BB  mov         ecx,offset _74AEDF8A_ReadWirteChar@c (0DEC008h)  
00DE17C0  call        @__CheckForDebuggerJustMyCode@4 (0DE132Fh)  
00DE17C5  nop  
	int ch1, ch2;

	ch1 = getchar();		// 문자 입력
00DE17C6  mov         esi,esp  
00DE17C8  call        dword ptr [__imp__getchar (0DEB174h)]  
00DE17CE  cmp         esi,esp  
00DE17D0  call        __RTC_CheckEsp (0DE124Eh)  
00DE17D5  mov         dword ptr [ch1],eax  
	ch2 = fgetc(stdin);		// 엔터키 입력
00DE17D8  mov         esi,esp  
00DE17DA  push        0  
00DE17DC  call        dword ptr [__imp____acrt_iob_func (0DEB180h)]  
00DE17E2  add         esp,4  
00DE17E5  cmp         esi,esp  
00DE17E7  call        __RTC_CheckEsp (0DE124Eh)  
00DE17EC  mov         esi,esp  
00DE17EE  push        eax  
00DE17EF  call        dword ptr [__imp__fgetc (0DEB17Ch)]  
00DE17F5  add         esp,4  
00DE17F8  cmp         esi,esp  
00DE17FA  call        __RTC_CheckEsp (0DE124Eh)  
00DE17FF  mov         dword ptr [ch2],eax  

	putchar(ch1);			// 문자 출력
00DE1802  mov         esi,esp  
00DE1804  mov         eax,dword ptr [ch1]  
00DE1807  push        eax  
00DE1808  call        dword ptr [__imp__putchar (0DEB170h)]  
00DE180E  add         esp,4  
00DE1811  cmp         esi,esp  
00DE1813  call        __RTC_CheckEsp (0DE124Eh)  
00DE1818  nop  
	fputc(ch2, stdout);		// 엔터키 출력
00DE1819  mov         esi,esp  
00DE181B  push        1  
00DE181D  call        dword ptr [__imp____acrt_iob_func (0DEB180h)]  
00DE1823  add         esp,4  
00DE1826  cmp         esi,esp  
00DE1828  call        __RTC_CheckEsp (0DE124Eh)  
00DE182D  mov         esi,esp  
00DE182F  push        eax  
00DE1830  mov         eax,dword ptr [ch2]  
00DE1833  push        eax  
00DE1834  call        dword ptr [__imp__fputc (0DEB178h)]  
00DE183A  add         esp,8  
00DE183D  cmp         esi,esp  
00DE183F  call        __RTC_CheckEsp (0DE124Eh)  
00DE1844  nop  

	return 0;
00DE1845  xor         eax,eax  
}
00DE1847  pop         edi  
00DE1848  pop         esi  
00DE1849  pop         ebx  
00DE184A  add         esp,0D8h  
00DE1850  cmp         ebp,esp  
00DE1852  call        __RTC_CheckEsp (0DE124Eh)  
00DE1857  mov         esp,ebp  
00DE1859  pop         ebp  
00DE185A  ret

 


 

#include <stdio.h>

int main(void)
{
00D91880  push        ebp  
00D91881  mov         ebp,esp  
00D91883  sub         esp,0CCh  
00D91889  push        ebx  
00D9188A  push        esi  
00D9188B  push        edi  
00D9188C  lea         edi,[ebp-0Ch]  
00D9188F  mov         ecx,3  
00D91894  mov         eax,0CCCCCCCCh  
00D91899  rep stos    dword ptr es:[edi]  
00D9189B  mov         ecx,offset _FFEA8C6C_WirteString@c (0D9C008h)  
00D918A0  call        @__CheckForDebuggerJustMyCode@4 (0D9132Fh)  
00D918A5  nop  
	char* str = "Simple String";
00D918A6  mov         dword ptr [str],offset string "Simple String" (0D97B30h)  

	printf("1. puts test ------ \n");
00D918AD  push        offset string "1. puts test ------ \n" (0D97B40h)  
00D918B2  call        _printf (0D910D7h)  
00D918B7  add         esp,4  
	puts(str);
00D918BA  mov         esi,esp  
00D918BC  mov         eax,dword ptr [str]  
00D918BF  push        eax  
00D918C0  call        dword ptr [__imp__puts (0D9B174h)]  
00D918C6  add         esp,4  
00D918C9  cmp         esi,esp  
00D918CB  call        __RTC_CheckEsp (0D91253h)  
00D918D0  nop  
	puts("So Simple String");
00D918D1  mov         esi,esp  
00D918D3  push        offset string "So Simple String" (0D97B5Ch)  
00D918D8  call        dword ptr [__imp__puts (0D9B174h)]  
00D918DE  add         esp,4  
00D918E1  cmp         esi,esp  
00D918E3  call        __RTC_CheckEsp (0D91253h)  
00D918E8  nop  

	printf("2. puts test ------ \n");
00D918E9  push        offset string "2. puts test ------ \n" (0D97B70h)  
00D918EE  call        _printf (0D910D7h)  
00D918F3  add         esp,4  
	fputs(str, stdout); printf("\n");
00D918F6  mov         esi,esp  
00D918F8  push        1  
00D918FA  call        dword ptr [__imp____acrt_iob_func (0D9B17Ch)]  
00D91900  add         esp,4  
00D91903  cmp         esi,esp  
00D91905  call        __RTC_CheckEsp (0D91253h)  
00D9190A  mov         esi,esp  
00D9190C  push        eax  
00D9190D  mov         eax,dword ptr [str]  
00D91910  push        eax  
00D91911  call        dword ptr [__imp__fputs (0D9B178h)]  
00D91917  add         esp,8  
00D9191A  cmp         esi,esp  
00D9191C  call        __RTC_CheckEsp (0D91253h)  
00D91921  nop  
00D91922  push        offset string "\n" (0D97B8Ch)  
00D91927  call        _printf (0D910D7h)  
00D9192C  add         esp,4  
	fputs("So Simple String", stdout); printf("\n");
00D9192F  mov         esi,esp  
00D91931  push        1  
00D91933  call        dword ptr [__imp____acrt_iob_func (0D9B17Ch)]  
00D91939  add         esp,4  
00D9193C  cmp         esi,esp  
00D9193E  call        __RTC_CheckEsp (0D91253h)  
00D91943  mov         esi,esp  
00D91945  push        eax  
00D91946  push        offset string "So Simple String" (0D97B5Ch)  
00D9194B  call        dword ptr [__imp__fputs (0D9B178h)]  
00D91951  add         esp,8  
00D91954  cmp         esi,esp  
00D91956  call        __RTC_CheckEsp (0D91253h)  
00D9195B  nop  
00D9195C  push        offset string "\n" (0D97B8Ch)  
00D91961  call        _printf (0D910D7h)  
00D91966  add         esp,4  

	printf("3. end of main ------ \n");
00D91969  push        offset string "3. end of main ------ \n" (0D97B90h)  
00D9196E  call        _printf (0D910D7h)  
00D91973  add         esp,4  

	return 0;
00D91976  xor         eax,eax  
}
00D91978  pop         edi  
00D91979  pop         esi  
00D9197A  pop         ebx  
00D9197B  add         esp,0CCh  
00D91981  cmp         ebp,esp  
00D91983  call        __RTC_CheckEsp (0D91253h)  
00D91988  mov         esp,ebp  
00D9198A  pop         ebp  
00D9198B  ret

 


 

#include <stdio.h>

int main(void)
{
00C51870  push        ebp  
00C51871  mov         ebp,esp  
00C51873  sub         esp,0E0h  
00C51879  push        ebx  
00C5187A  push        esi  
00C5187B  push        edi  
00C5187C  lea         edi,[ebp-20h]  
00C5187F  mov         ecx,8  
00C51884  mov         eax,0CCCCCCCCh  
00C51889  rep stos    dword ptr es:[edi]  
00C5188B  mov         eax,dword ptr [__security_cookie (0C5A000h)]  
00C51890  xor         eax,ebp  
00C51892  mov         dword ptr [ebp-4],eax  
00C51895  mov         ecx,offset _240753C6_ReadString@c (0C5C008h)  
00C5189A  call        @__CheckForDebuggerJustMyCode@4 (0C5132Fh)  
00C5189F  nop  
	char str[7];
	int i;

	for (i = 0; i < 3; i++)
00C518A0  mov         dword ptr [i],0  
00C518A7  jmp         __$EncStackInitStart+36h (0C518B2h)  
00C518A9  mov         eax,dword ptr [i]  
00C518AC  add         eax,1  
00C518AF  mov         dword ptr [i],eax  
00C518B2  cmp         dword ptr [i],3  
00C518B6  jge         __$EncStackInitStart+62h (0C518DEh)  
	{
		//fgets(str, sizeof(str), stdin);
		gets(str);
00C518B8  lea         eax,[str]  
00C518BB  push        eax  
00C518BC  call        _gets (0C513CAh)  
00C518C1  add         esp,4  
		printf("Read %d: %s \n", i + 1, str);
00C518C4  lea         eax,[str]  
00C518C7  push        eax  
00C518C8  mov         ecx,dword ptr [i]  
00C518CB  add         ecx,1  
00C518CE  push        ecx  
00C518CF  push        offset string "Read %d: %s \n" (0C57B30h)  
00C518D4  call        _printf (0C510D2h)  
00C518D9  add         esp,0Ch  
	}
00C518DC  jmp         __$EncStackInitStart+2Dh (0C518A9h)  

	return 0;
00C518DE  xor         eax,eax  
}
00C518E0  push        edx  
00C518E1  mov         ecx,ebp  
00C518E3  push        eax  
00C518E4  lea         edx,ds:[0C51910h]  
00C518EA  call        @_RTC_CheckStackVars@8 (0C511EFh)  
00C518EF  pop         eax  
00C518F0  pop         edx  
00C518F1  pop         edi  
00C518F2  pop         esi  
00C518F3  pop         ebx  
00C518F4  mov         ecx,dword ptr [ebp-4]  
00C518F7  xor         ecx,ebp  
00C518F9  call        @__security_check_cookie@4 (0C51154h)  
00C518FE  add         esp,0E0h  
00C51904  cmp         ebp,esp  
00C51906  call        __RTC_CheckEsp (0C51253h)  
00C5190B  mov         esp,ebp  
00C5190D  pop         ebp  
00C5190E  ret

 

 


 

int main(void)
{
00AE1900  push        ebp  
00AE1901  mov         ebp,esp  
00AE1903  sub         esp,130h  
00AE1909  push        ebx  
00AE190A  push        esi  
00AE190B  push        edi  
00AE190C  lea         edi,[ebp-70h]  
00AE190F  mov         ecx,1Ch  
00AE1914  mov         eax,0CCCCCCCCh  
00AE1919  rep stos    dword ptr es:[edi]  
00AE191B  mov         eax,dword ptr [__security_cookie (0AEA000h)]  
00AE1920  xor         eax,ebp  
00AE1922  mov         dword ptr [ebp-4],eax  
00AE1925  mov         ecx,offset _728D6715_RemoveBSN@c (0AEC00Eh)  
00AE192A  call        @__CheckForDebuggerJustMyCode@4 (0AE1334h)  
00AE192F  nop  
	char str[100];
	printf("문자열 입력 : ");
00AE1930  push        offset string "\xb9\xae\xc0\xda\xbf\xad \xc0\xd4\xb7\xc2 : " (0AE7B30h)  
00AE1935  call        _printf (0AE10D2h)  
00AE193A  add         esp,4  
	fgets(str, sizeof(str), stdin);
00AE193D  mov         esi,esp  
00AE193F  push        0  
00AE1941  call        dword ptr [__imp____acrt_iob_func (0AEB17Ch)]  
00AE1947  add         esp,4  
00AE194A  cmp         esi,esp  
00AE194C  call        __RTC_CheckEsp (0AE1258h)  
00AE1951  mov         esi,esp  
00AE1953  push        eax  
00AE1954  push        64h  
00AE1956  lea         eax,[str]  
00AE1959  push        eax  
00AE195A  call        dword ptr [__imp__fgets (0AEB178h)]  
00AE1960  add         esp,0Ch  
00AE1963  cmp         esi,esp  
00AE1965  call        __RTC_CheckEsp (0AE1258h)  
00AE196A  nop  
	printf("길이 : %d, 내용 : %s\n", strlen(str), str);
00AE196B  lea         eax,[str]  
00AE196E  push        eax  
00AE196F  lea         ecx,[str]  
00AE1972  push        ecx  
00AE1973  call        _strlen (0AE1384h)  
00AE1978  add         esp,4  
00AE197B  push        eax  
00AE197C  push        offset string "\xb1\xe6\xc0\xcc : %d, \xb3\xbb\xbf\xeb : %s\n" (0AE7B44h)  
00AE1981  call        _printf (0AE10D2h)  
00AE1986  add         esp,0Ch  
	printf("=================\n");
00AE1989  push        offset string "=================\n" (0AE7BFCh)  
00AE198E  call        _printf (0AE10D2h)  
00AE1993  add         esp,4  

	RemoveBSN(str);
00AE1996  lea         eax,[str]  
00AE1999  push        eax  
00AE199A  call        _RemoveBSN (0AE112Ch)  
00AE199F  add         esp,4  
	printf("길이 : %d, 내용 : %s\n", strlen(str), str);
00AE19A2  lea         eax,[str]  
00AE19A5  push        eax  
00AE19A6  lea         ecx,[str]  
00AE19A9  push        ecx  
00AE19AA  call        _strlen (0AE1384h)  
00AE19AF  add         esp,4  
00AE19B2  push        eax  
00AE19B3  push        offset string "\xb1\xe6\xc0\xcc : %d, \xb3\xbb\xbf\xeb : %s\n" (0AE7B44h)  
00AE19B8  call        _printf (0AE10D2h)  
00AE19BD  add         esp,0Ch  
	printf("=================\n");
00AE19C0  push        offset string "=================\n" (0AE7BFCh)  
00AE19C5  call        _printf (0AE10D2h)  
00AE19CA  add         esp,4  

	return 0;
00AE19CD  xor         eax,eax  
}
00AE19CF  push        edx  
00AE19D0  mov         ecx,ebp  
00AE19D2  push        eax  
00AE19D3  lea         edx,ds:[0AE1A00h]  
00AE19D9  call        @_RTC_CheckStackVars@8 (0AE11F4h)  
00AE19DE  pop         eax  
00AE19DF  pop         edx  
00AE19E0  pop         edi  
00AE19E1  pop         esi  
00AE19E2  pop         ebx  
00AE19E3  mov         ecx,dword ptr [ebp-4]  
00AE19E6  xor         ecx,ebp  
00AE19E8  call        @__security_check_cookie@4 (0AE1159h)  
00AE19ED  add         esp,130h  
00AE19F3  cmp         ebp,esp  
00AE19F5  call        __RTC_CheckEsp (0AE1258h)  
00AE19FA  mov         esp,ebp  
00AE19FC  pop         ebp  
00AE19FD  ret

 

 


 

https://learn.microsoft.com/ko-kr/cpp/c-runtime-library/reference/strcpy-s-wcscpy-s-mbscpy-s?view=msvc-170

 

strcpy_s, wcscpy_s, _mbscpy_s, _mbscpy_s_l

자세한 정보: strcpy_s, wcscpy_s, _mbscpy_s, _mbscpy_s_l

learn.microsoft.com

 

https://learn.microsoft.com/ko-kr/cpp/c-runtime-library/reference/strncpy-s-strncpy-s-l-wcsncpy-s-wcsncpy-s-l-mbsncpy-s-mbsncpy-s-l?view=msvc-170

 

strncpy_s, _strncpy_s_l, wcsncpy_s, _wcsncpy_s_l, _mbsncpy_s, _mbsncpy_s_l, _tcsncpy_s, _tcsncpy_s_l, _tcsnccpy_s, _tcsnccpy_s_l

자세한 정보: strncpy_s, _strncpy_s_l, wcsncpy_s, _wcsncpy_s_l, _mbsncpy_s, _mbsncpy_s_l

learn.microsoft.com

 

errno_t strcpy_s(
   char *dest,
   rsize_t dest_size,
   const char *src
);

errno_t strncpy_s(
   char *strDest,
   size_t numberOfElements,
   const char *strSource,
   size_t count
);

 


 

https://learn.microsoft.com/ko-kr/cpp/c-runtime-library/reference/strcat-s-wcscat-s-mbscat-s?view=msvc-170

 

strcat_s, wcscat_s, _mbscat_s, _mbscat_s_l

자세한 정보: strcat_s, wcscat_s, _mbscat_s, _mbscat_s_l

learn.microsoft.com

 

https://learn.microsoft.com/ko-kr/cpp/c-runtime-library/reference/strncat-s-strncat-s-l-wcsncat-s-wcsncat-s-l-mbsncat-s-mbsncat-s-l?view=msvc-170

 

strncat_s, _strncat_s_l, wcsncat_s, _wcsncat_s_l, _mbsncat_s, _mbsncat_s_l

자세한 정보: strncat_s, _strncat_s_l, wcsncat_s, _wcsncat_s_l, _mbsncat_s, _mbsncat_s_l

learn.microsoft.com

 

errno_t strcat_s(
   char *strDestination,
   size_t numberOfElements,
   const char *strSource
);

errno_t strncat_s(
   char *strDest,
   size_t numberOfElements,
   const char *strSource,
   size_t count
);