简体   繁体   English

使用scanf读取C中一定数量的字符?

[英]Using scanf to read in certain amount of characters in C?

I am having trouble accepting input from a text file. 我无法接受来自文本文件的输入。 My program is supposed to read in a string specified by the user and the length of that string is determined at runtime. 我的程序应该读取用户指定的字符串,并且该字符串的长度在运行时确定。 It works fine when the user is running the program (manually inputting the values) but when I run my teacher's text file, it runs into an infinite loop. 当用户正在运行程序(手动输入值)时,它运行良好,但是当我运行老师的文本文件时,它会陷入无限循环。

For this example, it fails when I am taking in 4 characters and his input in his file is "ABCDy". 对于此示例,当我输入4个字符并且他在文件中的输入为“ ABCDy”时,它失败。 "ABCD" is what I am supposed to be reading in and 'y' is supposed to be used later to know that I should restart the game. 我应该读“ ABCD”,以后再用“ y”知道我应该重新开始游戏。 Instead when I used scanf to read in "ABCD", it also reads in the 'y'. 相反,当我使用scanf读取“ ABCD”时,它也会读取“ y”。 Is there a way to get around this using scanf, assuming I won't know how long the string should be until runtime? 假设我在运行时之前不知道字符串应该有多长,有没有办法使用scanf解决此问题?

Normally, you'd use something like "%4c" or "%4s" to read a maximum of 4 characters (the difference is that "%4c" reads the next 4 characters, regardless, while "%4s" skips leading whitespace and stops at a whitespace if there is one). 通常,您将使用"%4c""%4s"之类的字符最多读取4个字符(不同之处在于, "%4c"将读取接下来的4个字符,而"%4s"跳过前导空格和如果有空格,则停在空白处)。

To specify the length at run-time, however, you have to get a bit trickier since you can't use a string literal with "4" embedded in it. 但是,要在运行时指定长度,您将需要一点技巧,因为您不能使用嵌入了“ 4”的字符串文字。 One alternative is to use sprintf to create the string you'll pass to scanf : 一种替代方法是使用sprintf创建要传递给scanf的字符串:

char buffer[128];

sprintf(buffer, "%%%dc", max_length);
scanf(buffer, your_string);

I should probably add: with printf you can specify the width or precision of a field dynamically by putting an asterisk ( * ) in the format string, and passing a variable in the appropriate position to specify the width/precision: 我可能应该添加:使用printf您可以通过在格式字符串中添加星号( * ),并在适当的位置传递变量以指定宽度/精度来动态指定字段的宽度或精度:

int width = 10;
int precision = 7;
double value = 12.345678910;

printf("%*.*f", width, precision, value);

Given that printf and scanf format strings are quite similar, one might think the same would work with scanf . 鉴于printfscanf格式字符串非常相似,人们可能会认为scanf可以使用相同的格式。 Unfortunately, this is not the case--with scanf an asterisk in the conversion specification indicates a value that should be scanned, but not converted. 不幸的是,情况并非如此-转换规范中带有scanf的星号表示应扫描但不能转换的值。 That is to say, something that must be present in the input, but its value won't be placed in any variable. 也就是说,某些内容必须存在于输入中,但其值不会放在任何变量中。

尝试

scanf("%4s", str)

You can also use fread, where you can set a read limit: 您还可以使用fread,在其中可以设置读取限制:

char string[5]={0};
if( fread(string,(sizeof string)-1,1,stdin) )
  printf("\nfull readed: %s",string);
else
  puts("error");

您可能会考虑简单地遍历对getc()的调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM