简体   繁体   English

(C/C++) fscanf_s Missing Integer Arguments 从 txt 文件中读取字符到数组时出错

[英](C/C++) fscanf_s Missing Integer Arguments error When Reading in Chars to Array from txt file

First Post, will do my best to follow guidelines and make this a proper setup.第一篇文章,将尽我所能遵循指导方针并进行适当的设置。 If ANYTHING is needed, let me know!如果需要任何东西,请告诉我!

The error in VS 2019 is "C6064: Missing integer argument to 'fscanf' that corresponds to conversion specificer '2'." VS 2019 中的错误是“C6064: Missing integer 参数到 'fscanf' 对应于转换规范 '2'。” The code below is given in the part where my code breaks.下面的代码在我的代码中断的部分给出。 The rest up to this point is fine and compiles correctly.到目前为止,rest 没有问题并且可以正确编译。 The line in error is the fscanf.出错的行是 fscanf。

ENTIRE CODE: https://pastebin.com/DCgEa64g (Excuse any missing variables, they have been dealt with!)完整代码: https://pastebin.com/DCgEa64g (请原谅任何缺少的变量,它们已被处理!)

fp = fopen("codefile.txt", "r");
if (fp == NULL)
{
    printf("could not open codefile.txt\n");
    return 1;
}

i = 0;
while (!feof(fp))
{

    fscanf(fp, "%c", &code[i]);
    i++;
}

This portion I posted is me scanning a file, codefile.txt, and saving each character inside to a string array, then comparing that array to a "ciphered message" to get an answer.我发布的这一部分是我扫描一个文件 codefile.txt,并将其中的每个字符保存到一个字符串数组中,然后将该数组与“加密消息”进行比较以获得答案。 The code compiles the correct answer, but I can't get this error to go away.该代码编译了正确的答案,但我无法将此错误传递给 go 。 The other message present is "'fscanf': not enough arguments passed for the format string" but I assumed only %c was needed for a character in a string array?出现的另一条消息是“'fscanf':没有足够的 arguments 为格式字符串传递”但我假设字符串数组中的字符只需要 %c?

This also puts each element in codefile.txt into the code array individually, which IS intended.这也将 codefile.txt 中的每个元素单独放入代码数组中,这是预期的。 I finished the rest of this assignment, so I'm not asking for somebody to do anything else at all for me.我完成了这项任务的 rest,所以我根本不要求有人为我做任何其他事情。 I will gladly post any other code needed, The goal is to read in a separate message file with numbers, put them into an array.我很乐意发布任何其他需要的代码,目标是读取带有数字的单独消息文件,将它们放入数组中。 descramble and compare to the codefile.txt string and get the result, I did 95% of the work, and need some help: Any clarification, just ask!解扰并与 codefile.txt 字符串进行比较并得到结果,我完成了 95% 的工作,需要一些帮助:任何澄清,请问! Turning this in tomorrow evening :)明天晚上交这个:)

Your actual code (at the link, not in your question) uses fscanf_s() , whose documentation says:您的实际代码(在链接中,而不是在您的问题中)使用fscanf_s() ,其文档说:

The main difference between the more secure functions (that have the _s suffix) and the other versions is that the more secure functions require the size in characters of each c, C, s, S, and [ type field to be passed as an argument immediately following the variable.更安全的函数(具有 _s 后缀)与其他版本之间的主要区别在于,更安全的函数需要将每个 c、C、s、S 和 [ 类型字段作为参数传递的字符大小紧跟在变量之后。 For more information, see scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l and scanf Width Specification.有关详细信息,请参阅 scanf_s、_scanf_s_l、wscanf_s、_wscanf_s_l 和 scanf 宽度规范。

Your error is in failing to provide the additional size argument.您的错误在于未能提供额外的尺寸参数。

Thank You John Zwinck;谢谢约翰·兹温克; I eventually got it working through adding SIZE to the argument when using fscanf_s specifically.在专门使用 fscanf_s 时,我最终通过在参数中添加 SIZE 来实现它。 Answer looks like this:答案如下所示:

    while (!feof(fp))
{
    fscanf_s(fp, "%c", &code[i], SIZE); //SIZE was added here!
    i++;
}

Thanks for the help!谢谢您的帮助!

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

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