C99-vscanf是否有假人? [英] C99 - vscanf for dummies?

查看:91
本文介绍了C99-vscanf是否有假人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉打扰S.O.要求提供一般信息。

I am sorry to bother S.O. with such a general request for information.

我可以找到很多vscanf术语非常繁杂的定义-但是我在具体示例中找不到很多东西,这些例子将显示从vscanf返回的内容。功能,除了IBM信息中心网站上的一个非常不错的功能:

I can find plenty of very terminology-heavy definitions of vscanf - but I can't find much in the way of concrete examples which will show what is being returned from the function, other than this quite nice one on the IBM infocenter site:

http://pic.dhe.ibm.com/infocenter/iseries/v7r1m0/index.jsp?topic=%2Frtref%2Fvscanf。 htm

但是,此示例仅适用于单行字符串输入,而我试图做同样的事情,但是要读取.txt文件

However this example is only for a one-line string input, whereas I am trying to do the same thing, but reading from a .txt file with multiple lines.

我是否必须使用fgets()一次读取一行,然后使用vscanf?我是否必须通过将函数放入函数vread()内来像IBM Infocenter示例一样在函数内利用它?

Do I have to use fgets() to read a line at a time and then use vscanf? Do I have to utilise it inside a function, the way that the IBM infocenter example does, by putting it inside a function vread()?

我是第一年CS学生,但这不是分配作业的问题,我只是想做一些额外的工作并扩大我的知识,而我们还没有vread / vscanf。

I am a first-year CS student, but this is not a question for an assignment, I am just trying to do some extra work and expand my knowledge, and we haven't got to vread/vscanf yet.

我应该坚持使用sscanf吗?

Should I just stick to using sscanf? My apologies if this is as stupid as that.

P.S。我很抱歉。我喜欢学习C。这是我31年以来在这个星球上学习的最大乐趣,除了我曾经与来自RSC的一些人一起参加莎士比亚研讨会。

P.S. I love learning C. Most fun I have had as a student in my 31 years on this planet, apart from when I did a Shakespeare workshop with some people from the RSC once.

推荐答案

vscanf类似于scanf,而不是sscanf或fscanf。它与scanf的不同之处在于scanf接受可变数量的参数,而vscanf接受va_list参数,该参数充当指向可变参数列表的指针。 vscanf的使用很少。它主要是为了保持完整性(更常见的是使用vprintf)。使用vscanf的一个可能示例是,如果您有大量带有不同参数的scanf调用,并且在每种情况下,如果scanf无法读取其所有参数,则它都会采取一些错误操作(相同的错误操作)。然后,您可以编写一个scanf_and_check_for_error函数,该函数像scanf一样接受可变参数,并添加一个count参数,该参数是scanf的预期返回值,将参数列表传递给vscanf,检查其返回值,如果返回值是错误的。例如,

vscanf is like scanf, not sscanf or fscanf. It differs from scanf in that scanf takes a variable number of arguments, whereas vscanf takes a va_list argument that acts as a pointer to a variable argument list. The use of vscanf is rare; it's mostly there for completeness (uses of vprintf are more common). A possible example of a use for vscanf is if you had a large number of scanf calls with different arguments, and on each occasion it takes some error action -- the same error action -- if scanf can't read all its arguments. Then you could write a scanf_and_check_for_error function that takes variable arguments just like scanf does, with the addition of a count argument that is the expected return value of scanf, passes the argument list to vscanf, checks its return value and takes the error action if the return value is wrong. e.g.,

void scanf_and_check(int count, char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    int rc = vscanf(fmt, ap);
    if (rc != count)
    {
        ErrorMessageBox("Bad input. This program employs a radical form of the fail-fast principle ... exiting!");
        exit(1);
    }
    va_end(ap);
}

这篇关于C99-vscanf是否有假人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆