scanf 错误处理 C

发布于 2024-11-07 06:57:01 字数 238 浏览 0 评论 0 原文

假设如果我想要一个输入,

[Name] [Name]

我将如何检测

[Name] [Name] [Name] 

并返回错误?

这是我到目前为止所拥有的,

char in[20];
char out[20];
scanf(" %s %s", out, in);

Say If i want an input to be

[Name] [Name]

How would I detect

[Name] [Name] [Name] 

and return error?

Here is what I have so far,

char in[20];
char out[20];
scanf(" %s %s", out, in);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离笑几人歌 2024-11-14 06:57:01

scanf 返回有效转换的参数的数量。因此,在第一种情况下,返回值将为 2,在后一种情况下为 3。

要检查参数的正确数量,这可能会有所帮助:

char in[20];
char out[20];
char error[20];
int check;

check = scanf(" %s %s %s", out, in, error);
if(check != 2)
{
    // TODO: error handling
}

编辑:现在它应该可以工作,请参阅下面的评论。

当然,正如其他海报所述: scanf 不被认为是一个非常安全的函数,因为可能会发生缓冲区溢出,您应该避免使用它。最好使用 fgets() 读取缓冲区的输入并尝试解析所需的参数。

scanf returns the number of validly converted arguments. So in your first case, the return value would be 2, in the latter case 3.

To check the right amount of parameters, this might help:

char in[20];
char out[20];
char error[20];
int check;

check = scanf(" %s %s %s", out, in, error);
if(check != 2)
{
    // TODO: error handling
}

EDIT: now it should be working, see comments below.

Of course, as stated by other posters: scanf is not considered a quite safe function since buffer overflows can occur, and you should avoid using it. It is better to read the inputs to a buffer with fgets() and the try to parse the arguments you want.

他是夢罘是命 2024-11-14 06:57:01

这是家庭作业,因此您可能需要在某些(任意)限制下工作。然而,短语“scanf 错误处理”在 C 编程语言中有些矛盾。

执行此操作的最佳方法是读取一行/其他合适的块并使用 C 字符串函数对其进行解析。您可以在一行 scanf 中完成此操作,但有很多缺点:

  1. 您可以防止缓冲区溢出,但如果缓冲区不够大,您将无法恢复。
  2. 您可以为字符串指定特定的字符范围,但它开始看起来有点正则表达式,并且 scanf"%[" 格式的行为在标准。
  3. 您可以检查第三个名称,但代码看起来不直观 - 它看起来不像您只需要两个名称,而看起来您需要三个。 scanf 还让您无法控制如何处理空白。

编辑:我最初从你的问题中认为这些名称包含在括号中(a la “[Bruce] [Wayne]”),但现在看来这只是你表示占位符的惯例。

不管怎样,尽管我非常不喜欢 scanf,但它也有它的用处。 (对我来说)最大的杀手是无法区分行尾和简单的空间分隔。要解决此问题,您可以调用 fgets 将数据读入缓冲区,然后在缓冲区上调用sscanf。这为您提供了 a) 更安全的读取(scanf 与其他更简单的函数从缓冲区读取的能力相混淆)和 b) scanf 格式的好处。

如果您必须使用 scanf,您的格式基本上是这样的:

" %s %s %s"

第三个是不可取的。正如 @Constantinius 的答案所示,您需要将数据读入三个缓冲区,并检查第三个缓冲区是否通过。但是,如果您正在读取此数据的多个连续行,则下一行的第一个条目将满足第三个槽,从而错误地给出错误。我强烈建议使用 fgetssscanf 或放弃 sscanf 在这种情况下进行更精确的手动解析。

以下是 fgets 手册页的链接 如果你错过了我之前偷偷溜进来的那个。如果您决定放弃 sscanf,请研究以下一些其他函数:strchr(或strspn,或 strcspn) 来查找名称的长度, strcpymemcpy< /code> (但请不要使用 strncpy,它不是您想象的那样)将数据复制到缓冲区中。

This is homework, so you might be required to work under certain (arbitrary) restrictions. However, the phrase "scanf error handling" is something of an oxymoron in the C programming language.

The best way to do this is to read in a line/other suitable chunk and parse it with C string functions. You can do it in one line of scanf but there are many drawbacks:

  1. You can guard against buffer overflows, but if a buffer isn't large enough you can't recover.
  2. You can specify specific character ranges for your strings, but it starts to look a little regexy, and the behavior of the "%[" format of scanf isn't mandated in the standard.
  3. You can check for a third name, but the code looks unintuitive - it doesn't look like you only want two names, it looks like you want three. scanf also gives you very little control over how you handle whitespace.

EDIT: I initially thought from your question that the names were contained in brackets (a la "[Bruce] [Wayne]") but it now appears that was merely your convention for denoting a placeholder.

Anyway, despite my intense dislike of scanf, it has its uses. The biggest killer (for me) is the inability to distinguish between line endings and simple space separation. To fix that, you can call fgets to read the data into a buffer, then call sscanf on the buffer. This gives you both a) safer reading (scanf messes with the ability of other more straightforward functions to read from a buffer) and b) the benefits of scanf formats.

If you have to use scanf, your format basically be this:

" %s %s %s"

With the third being undesirable. As @Constantinius's answer shows, you'd need to read data into three buffers, and check whether or not the third passed. However, if you're reading multiple consecutive lines of this data, then the first entry of the next line would satisfy the third slot, falsely giving you an error. I highly recommend using fgets and sscanf or ditching the sscanf for more precise manual parsing in this case.

Here's a link to the fgets man page if you missed the one I snuck in earlier. If you decide to ditch sscanf, here are some other functions to look into: strchr (or strspn, or strcspn) to find how long the name is, strcpy or memcpy (but please not strncpy, it's not what you think it is) to copy data into the buffers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文