/* Scanf.c: 1. Shows how to write a function to input data. 2. Shows how to retrieve a string. 3. Shows how to use C function: sscanf, or just use Scanf(). ** MSC 6.0 cannot use Scanf(), because don't support function vsscanf() Compiler: BC++ 3.1, Turbo C++ 1.01 (3.01) Compile mode: Large Project: Scanf.C ..\..\Lib\uPAC5000.lib Hordware: uPAC-5000 Note The COM1 of £gPAC-5000 is the standard I/O port. Before calling the InstallCom(1,.. ) whitin user's program, user can use the following member functions to send/receive data to/from COM1. Kbhit() Getch() Putch() Print() Scanf() All member functions do not work after Installcom(1,.. ) is called. It must use IsCom(1,.. ), ToCom(1,.. ), ReadCom(1,.. ) to send/receive data to/from COM1. These functions work until RestoreCom(1,.. ) is called. [Dec 28, 2011] by Liam */ #include #include #include "..\..\lib\upac5000.h" char buf[100]; void main(void) { int inumber, i2; char string[80]; int inputData; InitLib(); /* InitLib() must be called before other functions in the library may be used */ // method 1: use LineInput + sscanf Puts("Please input integer(-32768 ~ +32767), string: "); inputData=LineInput(buf, 99); if(inputData) { sscanf(buf, "%d %s", &inumber, string); Print("inumber=%d string=%s", inumber, string); } else { Puts("There is not any input.\r\n"); } // method 2: use Scanf // user can try to key in 0, 1, 2 or 3 item, to check the return value of Scanf SetScanBuffer(buf, 99); // set new buffer, so after call Scanf, can get the input string while(1) { Puts("\r\nPlease input integer (-32768 ~ +32767), string, integer again (input null space to quit): "); inputData=Scanf("%d %s %d", &inumber, string, &i2); if(buf[0]>='0' && buf[0]<='9') { if(inputData==1) { Print("inputData=%d integer=%d", inputData, inumber); Print("\n\rThe input string is: %s\r\n", buf); } else if(inputData==2) { Print("inputData=%d integer=%d string=%s", inputData, inumber, string); Print("\n\rThe input string is: %s\r\n", buf); } else if(inputData==3) { Print("inputData=%d integer1=%d string=%s integer2=%d", inputData, inumber, string, i2); Print("\n\rThe input string is: %s\r\n", buf); } } else if(buf[0]==' ') { break; } else { Print("The first character of string is not an integer.\r\n"); } } ResetScanBuffer(); }