#include <stdio.h>
#include <conio.h>
#include <windows.h> //MessageBeep
//控制函数
int GetStringEX(char* cpBuffer,int nMaxChars)
{
int cInput, nCount = 0;
while( (cInput = getch()) != '\r' )
{
if(cInput == '\b') //BackSpace
{
if(nCount > 0)
{
nCount--, printf("\b \b");
if(nCount > 0 && cpBuffer[nCount]<0)
nCount--, printf("\b \b");
}
}
else if(nCount < nMaxChars && (cInput < 0x80 || nCount + 1 < nMaxChars))
{
putchar( cpBuffer[nCount++] = (char)cInput );
if(cInput >= 0x80)
putchar( cpBuffer[nCount++] = (char)getch() );
}
else MessageBeep(MB_OK); //overflow BEEP
}
putchar('\n');
cpBuffer[nCount] = '\0';
return nCount;
}
//主函数调用示例:
int main()
{
char a[8];
GetStringEX(a,7); //最大长度7字符
puts(a);
getch();
return 0;
}