c语言scanf中使用%s的问题

horsley 2011-01-14 08:51:46
今天发现一问题搜索无果在此提问。
scanf中混合使用多个格式控制符例如:
scanf("%d,%s,%c,%d",&p1->num,p1->name,&p1->sex,&p1->age);
输入的时候我输入
1,tom,m,20(回车)
结果tom,m,20整个部分都赋值给了p1->name
如果不改变输入顺序,有什么办法可以解决。
(把%s放到最后面可以解决)
...全文
2271 39 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
39 条回复
切换为时间正序
请发表友善的回复…
发表回复
沛沛睡了 2013-05-17
  • 打赏
  • 举报
回复
scanf("%d,%s,%c,%d",&p1->num,p1->name,&p1->sex,&p1->age); 这种费力不讨好的方式请停止使用好吗? 增强你的交互性 可以这样 先提问:你的学号(1)?然后等待输入 再提问:你的名字(tom)?等待输入 提问:你的性别(m或f)? 等输入 问:你的年龄(2)?输入 重要的是要告诉别人:你需要别人输入什么。 更重要的是,你得到了你能够正确处理的数据。 这个问题不是一个研究语法的问题 这是思维的一个障碍。 请跨越。
14号选手 2013-05-17
  • 打赏
  • 举报
回复
引用 10 楼 qq120848369 的回复:
一楼那是神马,谁能解释,给我一个学习的机会.
他那种写法经常用在sscanf这个函数里面,一般以什么样的格式来对字符串进行赋值
fengqiuhuang1994 2013-05-17
  • 打赏
  • 举报
回复
','不是正确的scanf输入语句的结束标志。貌似是因为这样出错的,但是怎么解决不知道。 话说一楼看起来好厉害的样子,反正我没看懂,只会C语言的渣渣路过。
赵4老师 2011-06-22
  • 打赏
  • 举报
回复
以下内容摘自MSDN98:
scanf Width Specification
width is a positive decimal integer controlling the maximum number of characters to be read from stdin. No more than width characters are converted and stored at the corresponding argument. Fewer than width characters may be read if a white-space character (space, tab, or newline) or a character that cannot be converted according to the given format occurs before width is reached.

The optional prefixes h, l, I64, and L indicate the “size” of the argument (long or short, single-byte character or wide character, depending upon the type character that they modify). These format-specification characters are used with type characters in scanf or wscanf functions to specify interpretation of arguments as shown in the Table R.7. The type prefixes h, l, I64, and L are Microsoft extensions and are not ANSI-compatible. The type characters and their meanings are described in Table R.8.

Table R.7 Size Prefixes for scanf and wscanf Format-Type Specifiers

To Specify Use Prefix With Type Specifier
double l e, E, f, g, or G
long int l d, i, o, x, or X
long unsigned int l u
short int h d, i, o, x, or X
short unsigned int h u
__int64 I64 d, i, o, u, x, or X
Single-byte character with scanf h c or C
Single-byte character with wscanf h c or C
Wide character with scanf l c or C
Wide character with wscanf l c, or C
Single-byte – character string with scanf h s or S
Single-byte – character string with wscanf h s or S
Wide-character string with scanf l s or S
Wide-character string with wscanf l s or S


Following are examples of the use of h and l with scanffunctions and wscanf functions:

scanf( "%ls", &x ); // Read a wide-character string
wscanf( "%lC", &x ); // Read a single-byte character

To read strings not delimited by space characters, a set of characters in brackets ([ ]) can be substituted for the s (string) type character. The corresponding input field is read up to the first character that does not appear in the bracketed character set. If the first character in the set is a caret (^), the effect is reversed: The input field is read up to the first character that does appear in the rest of the character set.

Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]. This is a common scanf function extension, but note that the ANSI standard does not require it.
To store a string without storing a terminating null character ('\0'), use the specification %nc where n is a decimal integer. In this case, the c type character indicates that the argument is a pointer to a character array. The next n characters are read from the input stream into the specified location, and no null character ('\0') is appended. If n is not specified, its default value is 1.

The scanf function scans each input field, character by character. It may stop reading a particular input field before it reaches a space character for a variety of reasons:

The specified width has been reached.


The next character cannot be converted as specified.


The next character conflicts with a character in the control string that it is supposed to match.


The next character fails to appear in a given character set.
For whatever reason, when the scanf function stops reading an input field, the next input field is considered to begin at the first unread character. The conflicting character, if there is one, is considered unread and is the first character of the next input field or the first character in subsequent read operations on stdin.
OLDMAN_Tbag 2011-06-22
  • 打赏
  • 举报
回复
#include <stdio.h>

struct st_person
{
int num;
char name[20];
char sex;
int age;
};

int main(void)
{
struct st_person person;
struct st_person *p1 = &person;

scanf("%d,%[^,],%c,%d",&p1->num,p1->name,&p1->sex,&p1->age);

printf("%d,%s,%c,%d\n",p1->num, p1->name, p1->sex, p1->age);
return 0;
}
hotspring110 2011-06-22
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 schoolers 的回复:]

引用 7 楼 wizard_tiger 的回复:

scanf("%d%s%c%d",&amp;p1->num,p1->name,&amp;p1->sex,&amp;p1->age);
输入的时候输入:
1 tom m 20



或者按一楼的
[/Quote]
ww884203 2011-02-07
  • 打赏
  • 举报
回复
1楼很牛,我不知道可以这样。
但是下面的也说得好,可以多变通
HONGSEDAIMA1 2011-02-07
  • 打赏
  • 举报
回复
我觉得直接接受字符串, 然后在字符串中进行分析转化, 达到灵活通用。( 使用fgets(), fgetws()好像蛮好的, 这里强烈推荐使用fgetws(), 当然前面貌似需要设置些函数, 你懂地。 )
呃。 引用一本比较有名的书上的一句话( 不是完全匹配 ): C标准库中的scanf()和printf()不一用, scanf()有很强的副作用。 建议编写者自己实现输入语句。
ouotuo 2011-02-06
  • 打赏
  • 举报
回复
[Quote=引用楼主 horsleylee 的回复:]
今天发现一问题搜索无果在此提问。
scanf中混合使用多个格式控制符例如:
scanf("%d,%s,%c,%d",&p1->num,p1->name,&p1->sex,&p1->age);
输入的时候我输入
1,tom,m,20(回车)
结果tom,m,20整个部分都赋值给了p1->name
如果不改变输入顺序,有什么办法可以解决。
(把%s放到最后面可以解……
[/Quote]
+1
sea_spray 2011-02-05
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 prohibit 的回复:]
为了不造成不可预料的错误、还是把%s放到最后面吧~~
要嘛就换成两个scanf来控制输入~~
[/Quote]
+1
lei___ 2011-02-05
  • 打赏
  • 举报
回复
明白了。俺知道怎么注意了
kowity 2011-02-05
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 goolloo 的回复:]

引用楼主 horsleylee 的回复:
今天发现一问题搜索无果在此提问。
scanf中混合使用多个格式控制符例如:
scanf("%d,%s,%c,%d",&amp;p1->num,p1->name,&amp;p1->sex,&amp;p1->age);
输入的时候我输入
1,tom,m,20(回车)
结果tom,m,20整个部分都赋值给了p1->name
……
[/Quote]
这得看那个num是怎么定义的了,但讨论的重点不在这里,大家都是copy过来的
这你得看他的num是怎么定义的了。讨论的重点不在这里,所以后面这段大家
goolloo 2011-02-05
  • 打赏
  • 举报
回复
[Quote=引用楼主 horsleylee 的回复:]
今天发现一问题搜索无果在此提问。
scanf中混合使用多个格式控制符例如:
scanf("%d,%s,%c,%d",&p1->num,p1->name,&p1->sex,&p1->age);
输入的时候我输入
1,tom,m,20(回车)
结果tom,m,20整个部分都赋值给了p1->name
如果不改变输入顺序,有什么办法可以解决。
(把%s放到最后面可以解……
[/Quote]


不应该有“&”的吧?p1->num本身就是地址吧?
l369294289 2011-02-04
  • 打赏
  • 举报
回复
分开输
golden_eagle 2011-02-04
  • 打赏
  • 举报
回复
scanf( )函数默认读入若干项数据,数据之间是用空格、制表或回车换行分隔的。所以最好用空格分隔比较好,否则给自己增加麻烦。
muku9527 2011-02-03
  • 打赏
  • 举报
回复
神马都是浮云,你仔细看下 %s 的格式不就都知道了,哎哟一,楼主应该先学习下最基本的C 知识。将来必成大器!
Athenacle_ 2011-02-03
  • 打赏
  • 举报
回复
诶。。用空白分割就可以了。。
flysnowhite 2011-01-17
  • 打赏
  • 举报
回复
只能使用正则表达式。
gp341 2011-01-17
  • 打赏
  • 举报
回复
输入的字符串必须用空格或者回车分割才行 不能用逗号
mtj520 2011-01-17
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 dfatfat 的回复:]
引用 1 楼 niuchengshi 的回复:

scanf("%d,%[^,],%c,%d",&amp;p1->num,p1->name,&amp;p1->sex,&amp;p1->age);

高手
[/Quote]
+1
加载更多回复(18)

70,020

社区成员

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

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