看着段代码
#include <afx.h>
#include <stdio.h>int main ( )
{
int lo, hi;
CString str;
CStdioFile fFibo; fFibo.Open ("FIBO.DAT",CFile::modeWrite | CFile::modeCreate | CFile::typeText); str.Format ("%s\n", "Fibonacci sequencee, less than 100 :");//疑问printf函数是怎样和str配合工作的。
printf("%s",(LPCTSTR) str); //我想知道printf输出str的过程。
fFibo.WriteString(str); lo = hi = 1; str.Format ("%d\n", lo); //同上
printf ("%s", (LPCTSTR) str); //先谢谢各位仁兄了!
fFibo.WriteString (str); while (hi < 100)
{
str.Format("%d\n", hi);
printf ("%s", (LPCTSTR) str);
fFibo.WriteString (str);
hi = lo + hi;
lo = hi - lo;
} fFibo.Close();
return 0;
}
#include <afx.h>
#include <stdio.h>int main ( )
{
int lo, hi;
CString str;
CStdioFile fFibo; fFibo.Open ("FIBO.DAT",CFile::modeWrite | CFile::modeCreate | CFile::typeText); str.Format ("%s\n", "Fibonacci sequencee, less than 100 :");//疑问printf函数是怎样和str配合工作的。
printf("%s",(LPCTSTR) str); //我想知道printf输出str的过程。
fFibo.WriteString(str); lo = hi = 1; str.Format ("%d\n", lo); //同上
printf ("%s", (LPCTSTR) str); //先谢谢各位仁兄了!
fFibo.WriteString (str); while (hi < 100)
{
str.Format("%d\n", hi);
printf ("%s", (LPCTSTR) str);
fFibo.WriteString (str);
hi = lo + hi;
lo = hi - lo;
} fFibo.Close();
return 0;
}
选择使用 MFC就可以了(动态的和静态的都可以)
我是想知道:printf函数是怎样和str配合工作的。
来完成输出的。 (即工作过程)
{
ASSERT(AfxIsValidString(lpszFormat)); va_list argList;
va_start(argList, lpszFormat);
FormatV(lpszFormat, argList);
va_end(argList);
}
{
ASSERT(AfxIsValidString(lpszFormat)); va_list argListSave = argList; // make a guess at the maximum length of the resulting string
int nMaxLen = 0;
for (LPCTSTR lpsz = lpszFormat; *lpsz != '\0'; lpsz = _tcsinc(lpsz))
{
// handle '%' character, but watch out for '%%'
if (*lpsz != '%' || *(lpsz = _tcsinc(lpsz)) == '%')
{
nMaxLen += _tclen(lpsz);
continue;
} int nItemLen = 0; // handle '%' character with format
int nWidth = 0;
for (; *lpsz != '\0'; lpsz = _tcsinc(lpsz))
{
// check for valid flags
if (*lpsz == '#')
nMaxLen += 2; // for '0x'
else if (*lpsz == '*')
nWidth = va_arg(argList, int);
else if (*lpsz == '-' || *lpsz == '+' || *lpsz == '0' ||
*lpsz == ' ')
;
else // hit non-flag character
break;
}
// get width and skip it
if (nWidth == 0)
{
// width indicated by
nWidth = _ttoi(lpsz);
for (; *lpsz != '\0' && _istdigit(*lpsz); lpsz = _tcsinc(lpsz))
;
}
ASSERT(nWidth >= 0); int nPrecision = 0;
if (*lpsz == '.')
{
// skip past '.' separator (width.precision)
lpsz = _tcsinc(lpsz); // get precision and skip it
if (*lpsz == '*')
{
nPrecision = va_arg(argList, int);
lpsz = _tcsinc(lpsz);
}
else
{
nPrecision = _ttoi(lpsz);
for (; *lpsz != '\0' && _istdigit(*lpsz); lpsz = _tcsinc(lpsz))
;
}
ASSERT(nPrecision >= 0);
} // should be on type modifier or specifier
int nModifier = 0;
if (_tcsncmp(lpsz, _T("I64"), 3) == 0)
{
lpsz += 3;
nModifier = FORCE_INT64;
#if !defined(_X86_) && !defined(_ALPHA_)
// __int64 is only available on X86 and ALPHA platforms
ASSERT(FALSE);
#endif
}
else
{
switch (*lpsz)
{
// modifiers that affect size
case 'h':
nModifier = FORCE_ANSI;
lpsz = _tcsinc(lpsz);
break;
case 'l':
nModifier = FORCE_UNICODE;
lpsz = _tcsinc(lpsz);
break; // modifiers that do not affect size
case 'F':
case 'N':
case 'L':
lpsz = _tcsinc(lpsz);
break;
}
} // now should be on specifier
switch (*lpsz | nModifier)
{
// single characters
case 'c':
case 'C':
nItemLen = 2;
va_arg(argList, TCHAR_ARG);
break;
case 'c'|FORCE_ANSI:
case 'C'|FORCE_ANSI:
nItemLen = 2;
va_arg(argList, CHAR_ARG);
break;
case 'c'|FORCE_UNICODE:
case 'C'|FORCE_UNICODE:
nItemLen = 2;
va_arg(argList, WCHAR_ARG);
break; // strings
case 's':
{
LPCTSTR pstrNextArg = va_arg(argList, LPCTSTR);
if (pstrNextArg == NULL)
nItemLen = 6; // "(null)"
else
{
nItemLen = lstrlen(pstrNextArg);
nItemLen = max(1, nItemLen);
}
}
break; case 'S':
{
#ifndef _UNICODE
LPWSTR pstrNextArg = va_arg(argList, LPWSTR);
if (pstrNextArg == NULL)
nItemLen = 6; // "(null)"
else
{
nItemLen = wcslen(pstrNextArg);
nItemLen = max(1, nItemLen);
}
#else
LPCSTR pstrNextArg = va_arg(argList, LPCSTR);
if (pstrNextArg == NULL)
nItemLen = 6; // "(null)"
else
{
nItemLen = lstrlenA(pstrNextArg);
nItemLen = max(1, nItemLen);
}
#endif
}
break; case 's'|FORCE_ANSI:
case 'S'|FORCE_ANSI:
{
LPCSTR pstrNextArg = va_arg(argList, LPCSTR);
if (pstrNextArg == NULL)
nItemLen = 6; // "(null)"
else
{
nItemLen = lstrlenA(pstrNextArg);
nItemLen = max(1, nItemLen);
}
}
break; case 's'|FORCE_UNICODE:
case 'S'|FORCE_UNICODE:
{
LPWSTR pstrNextArg = va_arg(argList, LPWSTR);
if (pstrNextArg == NULL)
nItemLen = 6; // "(null)"
else
{
nItemLen = wcslen(pstrNextArg);
nItemLen = max(1, nItemLen);
}
}
break;
} // adjust nItemLen for strings
if (nItemLen != 0)
{
if (nPrecision != 0)
nItemLen = min(nItemLen, nPrecision);
nItemLen = max(nItemLen, nWidth);
}
else
{
switch (*lpsz)
{
// integers
case 'd':
case 'i':
case 'u':
case 'x':
case 'X':
case 'o':
if (nModifier & FORCE_INT64)
va_arg(argList, __int64);
else
va_arg(argList, int);
nItemLen = 32;
nItemLen = max(nItemLen, nWidth+nPrecision);
break; case 'e':
case 'g':
case 'G':
va_arg(argList, DOUBLE_ARG);
nItemLen = 128;
nItemLen = max(nItemLen, nWidth+nPrecision);
break; case 'f':
va_arg(argList, DOUBLE_ARG);
nItemLen = 128; // width isn't truncated
// 312 == strlen("-1+(309 zeroes).")
// 309 zeroes == max precision of a double
nItemLen = max(nItemLen, 312+nPrecision);
break; case 'p':
va_arg(argList, void*);
nItemLen = 32;
nItemLen = max(nItemLen, nWidth+nPrecision);
break; // no output
case 'n':
va_arg(argList, int*);
break; default:
ASSERT(FALSE); // unknown formatting option
}
} // adjust nMaxLen for output nItemLen
nMaxLen += nItemLen;
} GetBuffer(nMaxLen);
VERIFY(_vstprintf(m_pchData, lpszFormat, argListSave) <= GetAllocLength());
ReleaseBuffer(); va_end(argListSave);
}// formatting (using wsprintf style formatting)
Fortmat函数上面的大侠把代码都贴出来了,偶就不多说了。