windows编程

ruoyuruoni 2014-12-16 09:05:49
C语言:我需要从文本框中读取int型数据,但是文本框都是字符串类型,有什么办法可以解决这个问题吗。请求大神赐教
...全文
255 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-12-17
  • 打赏
  • 举报
回复
sscanf, swscanf Read formatted data from a string. int sscanf( const char *buffer, const char *format [, argument ] ... ); int swscanf( const wchar_t *buffer, const wchar_t *format [, argument ] ... ); Routine Required Header Compatibility sscanf <stdio.h> ANSI, Win 95, Win NT swscanf <stdio.h> or <wchar.h> ANSI, Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value Each of these functions returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. The return value is EOF for an error or if the end of the string is reached before the first conversion. Parameters buffer Stored data format Format-control string argument Optional arguments For more information, see Format Specifications. Remarks The sscanf function reads data from buffer into the location given by each argument. Every argument must be a pointer to a variable with a type that corresponds to a type specifier in format. The format argument controls the interpretation of the input fields and has the same form and function as the format argument for the scanf function; see scanf for a complete description of format. If copying takes place between strings that overlap, the behavior is undefined. swscanf is a wide-character version of sscanf; the arguments to swscanf are wide-character strings. sscanf does not handle multibyte hexadecimal characters. swscanf does not handle Unicode fullwidth hexadecimal or “compatibility zone” characters. Otherwise, swscanf and sscanf behave identically. Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _stscanf sscanf sscanf swscanf Example /* SSCANF.C: This program uses sscanf to read data items * from a string named tokenstring, then displays them. */ #include <stdio.h> void main( void ) { char tokenstring[] = "15 12 14..."; char s[81]; char c; int i; float fp; /* Input various data from tokenstring: */ sscanf( tokenstring, "%s", s ); sscanf( tokenstring, "%c", &c ); sscanf( tokenstring, "%d", &i ); sscanf( tokenstring, "%f", &fp ); /* Output the data read */ printf( "String = %s\n", s ); printf( "Character = %c\n", c ); printf( "Integer: = %d\n", i ); printf( "Real: = %f\n", fp ); } Output String = 15 Character = 1 Integer: = 15 Real: = 15.000000 Stream I/O Routines See Also fscanf, scanf, sprintf, _snprintf
ruoyuruoni 2014-12-17
  • 打赏
  • 举报
回复
囧么转换,为什么我转换得的结果是那种不认识的字符。我使用createwindow创建的edit,不是dialog哦
Evankaka 2014-12-17
  • 打赏
  • 举报
回复
CEdit *pEdit = (CEdit*)GetDlgItem(IDC_EDIT1);///IDC_EDIT1改成你的 CString str1; pEdit->GetWindowText(str1); int a = = _ttoi(str1);//将CString ->int
Evankaka 2014-12-16
  • 打赏
  • 举报
回复
4楼的方法可以做;将一个INT变量和文本框关联起来(这里电脑会自己帮你判断是否输入的是数字) 另一方法是每次要用时,获取文本框的内容,然后将其转化为INT(这里你要自己来判断输入是不是数字)
zacharyLiu 2014-12-16
  • 打赏
  • 举报
回复
不是有可以添加变量吗?添加变量,然后变量类型设置为int,不就可以啦?
Sping 2014-12-16
  • 打赏
  • 举报
回复

TCHAR tchText[64]={0}; 
GetDlgItemText(IDC_EDIT_ANTENNA_COMAZ,tchText,64);
int iEdit = _ttoi(tchText);

BOOL flag;
UINT ui=GetDlgItemInt(IDC_EDIT1,&flag,true);
ruoyuruoni 2014-12-16
  • 打赏
  • 举报
回复
是用哪一个组件,edit还是dlg,能不能给我一下核心代码
movsd 2014-12-16
  • 打赏
  • 举报
回复
GetDlgItemInt

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧