텍스트 IO - Win32
SetStdHandle()
SetConsoleMode()
ReadConsole()
WriteConsole()
FreeConsole()
AllocConsole()
#include <stdarg.h>
BOOL PrintStrings (HANDLE hOut, ...)
/* Write the messages to the output handle. Frequently hOut
will be standard out or error, but this is not required.
Use WriteConsole (to handle Unicode) first, as the
output will normally be the console. If that fails, use WriteFile.
hOut: Handle for output file.
... : Variable argument list containing TCHAR strings.
The list must be terminated with NULL. */
{
DWORD MsgLen, Count;
LPCTSTR pMsg;
va_list pMsgList; /* Current message string. */
va_start (pMsgList, hOut); /* Start processing msgs. */
while ((pMsg = va_arg (pMsgList, LPCTSTR)) != NULL) {
MsgLen = lstrlen (pMsg);
if (!WriteConsole (hOut, pMsg, MsgLen, &Count, NULL)
&& !WriteFile (hOut, pMsg, MsgLen * sizeof (TCHAR),
&Count, NULL))
return FALSE;
}
va_end (pMsgList);
return TRUE;
}
BOOL PrintMsg (HANDLE hOut, LPCTSTR pMsg)
/* For convenience only - Single message version of PrintStrings so that
you do not have to remember the NULL arg list terminator.
hOut: Handle of output file
pMsg: Single message to output. */
{
return PrintStrings (hOut, pMsg, NULL);
}
BOOL ConsolePrompt (LPCTSTR pPromptMsg, LPTSTR pResponse, DWORD MaxTchar, BOOL Echo)
/* Prompt the user at the console and get a response
which can be up to MaxTchar generic characters.
pPromptMessage: Message displayed to user.
pResponse: Programmer-supplied buffer that receives the response.
MaxTchar: Maximum size of the user buffer, measured as generic characters.
Echo: Do not display the user's response if this flag is FALSE. */
{
HANDLE hStdIn, hStdOut;
DWORD TcharIn, EchoFlag;
BOOL Success;
hStdIn = CreateFile (TEXT ("CONIN$"), GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
hStdOut = CreateFile (TEXT ("CONOUT$"), GENERIC_WRITE, 0,
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
/* Should the input be echoed? */
EchoFlag = Echo ? ENABLE_ECHO_INPUT : 0;
/* API "and" chain. If any test or system call fails, the
rest of the expression is not evaluated, and the
subsequent functions are not called. GetStdError ()
will return the result of the failed call. */
Success = SetConsoleMode (hStdIn, ENABLE_LINE_INPUT |
EchoFlag | ENABLE_PROCESSED_INPUT)
&& SetConsoleMode (hStdOut,
ENABLE_WRAP_AT_EOL_OUTPUT | ENABLE_PROCESSED_OUTPUT)
&& PrintStrings (hStdOut, pPromptMsg, NULL)
&& ReadConsole (hStdIn, pResponse, MaxTchar, &TcharIn, NULL);
/* Replace the CR-LF by the null character. */
if (Success)
pResponse [TcharIn - 2] = '\0';
CloseHandle (hStdIn);
CloseHandle (hStdOut);
return Success;
}
VOID ReportError (LPCTSTR UserMessage, DWORD ExitCode, BOOL PrintErrorMsg)
/* General-purpose function for reporting system errors.
Obtain the error number and turn it into the system error message.
Display this information and the user-specified message to the standard error device.
UserMessage: Message to be displayed to standard error device.
ExitCode: 0 - Return.
\x11> 0 - ExitProcess with this code.
PrintErrorMessage: Display the last system error message if this flag is set. */
{
DWORD eMsgLen, ErrNum = GetLastError ();
LPTSTR lpvSysMsg;
HANDLE hStdErr;
hStdErr = GetStdHandle (STD_ERROR_HANDLE);
PrintMsg (hStdErr, UserMessage);
if (PrintErrorMsg) {
eMsgLen = FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM, NULL,
ErrNum, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpvSysMsg, 0, NULL);
PrintStrings (hStdErr, TEXT ("\n"), lpvSysMsg, TEXT ("\n"), NULL);
HeapFree (GetProcessHeap (), 0, lpvSysMsg);
/* Explained in Chapter 5. */
}
if (ExitCode > 0)
ExitProcess (ExitCode);
else
return;
}
#define _UNICODE // C 라이브러리
#include <tchar.h>
_fgettc()
_tprintf()
_stprintf() // sprintf()
_tstcpy() // strcpy()
_itot() // itoa()
_ttoi()
_totuppper()
_totlower()
#include <stdio.h>
fread()
fwrite()
ferror()
feof()
fgetc()
fputc()
포매티드 IO(?)
FILE *fp;
fp = fopen("...", "w");
fprintf(fp, "%s\n%s\n%d\n%d\n%d", m_strProdName, m_strProdCode, m_nUnitCost, m_nQuantity, m_nTotalCost);
fclose(fp);
FILE *fp;
fp = fopen("...", "r");
char strProdName[20];
fscanf(fp, "%s\n", strProdName);
m_strProdName = strProdName;
fscanf(fp, "%d\n%d\n%d", &m_nUnitCost, &m_nQuantity, &m_nTotalCost);
fclose(fp);
순차적 파일 처리(?)
{
int c;
while ((c = getchar()) != EOF)
putchar(c);
return 0;
}
#define BUF_SIZE 0x200
static VOID CatFile (HANDLE, HANDLE);
int _tmain (int argc, LPTSTR argv [])
{
HANDLE hInFile, hStdIn = GetStdHandle (STD_INPUT_HANDLE);
HANDLE hStdOut = GetStdHandle (STD_OUTPUT_HANDLE);
BOOL DashS;
int iArg, iFirstFile;
iFirstFile = Options (argc, argv, _T ("s"), &DashS, NULL);
if (iFirstFile == argc) { /* No files in arg list. */
CatFile (hStdIn, hStdOut);
return 0;
}
for (iArg = iFirstFile; iArg < argc; iArg++) {
_tprintf (_T("FileName %s\n"), argv[iArg]);
hInFile = CreateFile (argv [iArg], GENERIC_READ,
0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hInFile == INVALID_HANDLE_VALUE && !DashS)
ReportError (_T ("Cat Error: File does not exist."), 1, TRUE);
CatFile (hInFile, hStdOut);
CloseHandle (hInFile);
}
return 0;
}
static VOID CatFile (HANDLE hInFile, HANDLE hOutFile)
{
DWORD nIn, nOut;
BYTE Buffer [BUF_SIZE];
while (ReadFile (hInFile, Buffer, BUF_SIZE, &nIn, NULL) && (nIn != 0)
&& WriteFile (hOutFile, Buffer, nIn, &nOut, NULL));
return;
}
#define MAXLINE 1000
int getline(char s[], int lim)
{
int c, i;
while (--lim > 0 && (c=getchar()) != EOF && c != '\n')
s[i++] = c;
if (c == '\n')
s[i++] = c;
s[i] = '\0';
return i;
}
int strindex(char s[], char t[])
{
int i, j, k;
for (i = 0; s[i] != '\0'; i++) {
for (j=i, k=0; t[k]!='\0' && s[j]==t[k]; j++, k++)
;
if (k > 0 && t[k] == '\0')
return i;
}
return -1;
}
int main(int argc, char* argv[])
{
char line[MAXLINE];
int found = 0;
char pattern[] = "ould";
while (getline(line, MAXLINE) > 0)
if (strindex(line, pattern) >= 0) {
printf("%s", line);
found++;
}
return found;
}